1

可能重复:
什么是未定义的引用/未解决的外部符号错误,我该如何解决?

我有一个游戏程序,我感到非常沮丧。一切运行良好,我决定通过为每组函数创建单独的文件来清理我的程序。代码很长,包含多个文件,但基本思想如下:

我在 Windows XP 上使用 Code::Blocks IDE

在我的 entity.h 中,我声明了该类的所有函数和变量。在我的 entity.cpp 中,我已经将它包含在我的所有其他文件中。但是我仍然收到大量错误,这些错误告诉我,我对 entity.h 中的所有方法以及所有其他头文件都有未定义的引用。例如,我有一个函数调用 print() 可以更轻松地打印出东西,这是我从 entity.h 文件中调用的第一个方法。我收到此错误:

下面是 print() 的代码:

void print(string f) {
 cout<<f<<endl;
} 

我怎么称呼它:

void Player::win(){
entity e;
e.print("You have defeated the orc");

} 错误:

In function 'ZN6Player3winEv': undefined reference to 'entity::print(std::string)'

是的,我确实有一个实体对象。它也发生在实体类和文件中的每个其他函数中。

4

2 回答 2

2
void print(string f) {
 cout<<f<<endl;
} 

应该

void entity::print(string f) {
 cout<<f<<endl;
} 
于 2012-10-13T22:13:38.163 回答
0
void print(string f) {
 cout<<f<<endl;
} 

是一个全局函数

如果你想打电话

e.print("You have defeated the orc");

那么你需要一个实现

void entity::print(string f)
于 2012-10-13T22:16:54.827 回答