0

I know that there are a lot of questions somewhat related to this one, but they answers are a bit hard for me to make sense of. I'm receiving the following error for a few different lines of code:

C:\Users\Jeff\AppData\Local\Temp\ccAixtmT.o:football.cpp:(.text+0x6f0): undefined 
reference to `Player::set_values(int, std::string, float)'

From these blocks of code:

class Player {
int playerNum;
string playerPos;
float playerRank;
public:
    void set_values(int, string, float);
    float get_rank(){ return playerRank; };
    bool operator == (const Player &p1/*, const Player &p2*/) const
    {
    if(&p1.playerNum == &playerNum &&
       &p1.playerPos == &playerPos &&
       &p1.playerRank == &playerRank)
       return true;
    else
       return false; };
};

And this being the main function referencing the subclass:

int main() {

ifstream infile;
infile.open ("input.txt", ifstream::in);
int numTeams;
string command;
while(!infile.fail() && !infile.eof()){
    infile >> numTeams;
    string name;
    Player p;
    int playNum;
    string playPos;
    float playRank;
    Player all[11];
    float ranks[11];
    Team allTeams[numTeams];
    for(int i=0; i<numTeams; i++){
        infile >> name;
        for(int j=0; j<11; j++){
            infile >> playNum;
            infile >> playPos;
            infile >> playRank;
            if(playPos == "QB")
                p.set_values(playNum, playPos, (playRank*2.0));
            else if(playPos == "RB")
                p.set_values(playNum, playPos, (playRank*1.5));
            else if(playPos == "WR")
                p.set_values(playNum, playPos, (playRank/1.8));
            else if(playPos == "TE")
                p.set_values(playNum, playPos, (playRank*1.1));
            else if(playPos == "GD")
                p.set_values(playNum, playPos, (playRank/2.0));
            else if(playPos == "TC")
                p.set_values(playNum, playPos, (playRank/2.2));
            else if(playPos == "CR")
                p.set_values(playNum, playPos, (playRank/1.2));
            all[j] = p;
            allTeams[i].set_values(all, name);
        }
    }
    infile >> command;
    if (command == "play"){ 
        int t1;
        int t2;
        infile >> t1;
        infile >> t2;
        play(allTeams[t1], allTeams[t2]); 
    }
    else { 
        int t1;
        int p1;
        int t2;
        int p2;
        swap(allTeams[t1], allTeams[t1].get_player(p1), allTeams[t2], allTeams[t2].get_player(p2)); }
}
}
2
  • 5
    Where do you define the implementation of set_values? Apr 13, 2012 at 22:51
  • I'm learning from some sample code that used the set_values function in this way, so I assumed that's how it worked. I suppose it's not, though. Haha.
    – serenus
    Apr 13, 2012 at 22:54

3 Answers 3

4

You declared set_values in your class, but never provided a body for it as you did with the others. When you call the function, there's nothing to execute!

3

Well, there's a couple of mistakes here but regarding you question, here's how the set_value should be implemented :

 void set_values(int playerNumParam, string playerPosParam, float playerRankParam){
     playerNum = playerNumParam;
     playerPos = playerPosParam;
     playerRank = playerRankParam;
}

See this link : Constructor and destructors

Also, for good practice, it is always a good idea to name your class-member variable by ending them with an underscore

playerNum_
playerPos_
playerRank_

Hope it helped!

0

Several different possible reasons for not being able to find a function.

One, you declared the class in another c++ file which means that the current c++ file cannot find it within the file that you declared main. Possible solution to this problem is either to declare the class and its functions in the same file as the main.

Two, you didn't actually implement/declare the function setvalues. Possible solution is to implement setvalues inside the class or implement it outside of the class.

Three, you declared the function and the class within a namespace but you didn't qualify the function.

Let me know if you need examples.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.