我有一个带有这一行的main.cpp文件:
#include "Player.hpp"
这是Player.hpp:
class Player {
private :
int type; // [0 = humain local, 1 = IA de niveau 1, 2 = IA de niveau 2, 3 = humain en réseau]
int score;
int color;
public :
Player();
Player (int type, int color);
int getType();
int getScore();
int getColor();
};
这是Player.cpp:
Player::Player() {
}
Player::Player(int type, int color) {
this->type = type;
this->color = color;
this->score = 0;
}
int Player::getType() {
return this->type;
}
int Player::getScore() {
return this->score;
}
int Player::getColor() {
return this->color;
}
如果我用这一行编译:
g++ Main.cpp -o main
它显示了错误:
Undefined symbols for architecture x86_64:
"Player::getColor()", referenced from: etc...........
但是,如果我将Player.cpp中的代码放在Player.hpp的代码下面,如下所示:
class Player {
private :
int type; // [0 = humain local, 1 = IA de niveau 1, 2 = IA de niveau 2, 3 = humain en réseau]
int score;
int color;
public :
Player();
Player (int type, int color);
int getType();
int getScore();
int getColor();
};
Player::Player() {
}
Player::Player(int type, int color) {
this->type = type;
this->color = color;
this->score = 0;
}
int Player::getType() {
return this->type;
}
int Player::getScore() {
return this->score;
}
int Player::getColor() {
return this->color;
}
它有效。
我该怎么做才能解决问题?我认为这是一个编译问题。
感谢您的帮助!
答案 0 :(得分:1)
你的命令,
g++ Main.cpp -o main
不会编译或链接Player.cpp文件,因此它正确地说它无法在Player.cpp文件中找到它所谈论的符号。
将Player.cpp添加到您的构建命令:
g++ Main.cpp Player.cpp -o main
答案 1 :(得分:-1)
Main.cpp引用了Player.hpp,但没有引用Player.cpp。添加行:
#include "Player.cpp"
应该解决这个问题