C++ “未定义的引用”
我知道有很多问题与此相关,但它们的答案对我来说有点难以理解。我收到以下几行不同代码的错误:C++ “未定义的引用”,c++,compiler-errors,C++,Compiler Errors,我知道有很多问题与此相关,但它们的答案对我来说有点难以理解。我收到以下几行不同代码的错误: C:\Users\Jeff\AppData\Local\Temp\ccAixtmT.o:football.cpp:(.text+0x6f0): undefined reference to `Player::set_values(int, std::string, float)' class Player { int playerNum; string playerPos; float playerR
C:\Users\Jeff\AppData\Local\Temp\ccAixtmT.o:football.cpp:(.text+0x6f0): undefined
reference to `Player::set_values(int, std::string, float)'
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; };
};
从这些代码块中:
C:\Users\Jeff\AppData\Local\Temp\ccAixtmT.o:football.cpp:(.text+0x6f0): undefined
reference to `Player::set_values(int, std::string, float)'
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; };
};
这是引用子类的主要函数:
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)); }
}
}
您在类中声明了set_值,但从未像处理其他对象一样为其提供主体。当你调用函数时,没有什么可执行的 这里有几个错误,但关于您的问题,下面是如何实现set_值:
void set_values(int playerNumParam, string playerPosParam, float playerRankParam){
playerNum = playerNumParam;
playerPos = playerPosParam;
playerRank = playerRankParam;
}
请参阅此链接:
此外,为了获得良好的实践效果,最好在类成员变量的末尾加下划线来命名它们
playerNum_
playerPos_
playerRank_
希望有帮助 找不到函数的几种可能原因
一,你在另一C++文件中声明了类,这意味着当前C++文件找不到你声明的文件中的主文件。这个问题的可能解决方案是在与main相同的文件中声明类及其函数
第二,您没有实际实现/声明函数setvalues。可能的解决方案是在类内部实现setvalues,或者在类外部实现setvalues 第三,您在名称空间中声明了函数和类,但没有限定函数如果你需要例子,请告诉我 您在哪里定义set_值的实现?我从一些示例代码中学习,这些代码以这种方式使用set_值函数,所以我假设它就是这样工作的。不过,我想不是。哈哈。