Why do I keep getting the error: "undefined reference to 'robots::robots()'
Why do I keep getting the error: "undefined reference to 'robots::robots()'
我正在创建一个程序来练习我在 类 和文件方面的知识,但我可以让它工作。以前我收到一条错误消息说 robots::robots() 被定义了多次,但现在我收到一条错误消息说未定义对 'robots::robots()' 的引用。这是 enemy_definitions.h 的代码:
#include <iostream>
using namespace std;
class enemies
{
public:
string name;
int hp;
int damage;
virtual void print_information();
};
class robots: public enemies
{
public:
robots();
void print_information();
private:
int power_requirement;
};
class zombies: public enemies
{
public:
void print_information();
private:
int height;
};
class aliens: public enemies
{
public:
void print_information();
private:
string colour;
};
这是 enemy_definitions.cpp 的代码:
#include <iostream>
#include "enemy_definitions.h"
void enemies :: print_information()
{
}
robots :: robots()
{
cout <<"Name: ";
cin >> name;
cout <<"\nhp: ";
cin >> hp;
cout <<"\ndamage: ";
cin >> damage;
cout <<"\n power_requirement: ";
cin >> power_requirement;
}
void robots :: print_information()
{
cout << this->name << " has ";
cout << this->hp << "hit-points, ";
cout << this->damage << " damage and ";
cout << this->power_requirement << "power requirement";
}
void zombies :: print_information()
{
cout << this->name << " has ";
cout << this->hp << "hit-points, ";
cout << this->damage << " damage and ";
cout << this->height << "height";
}
void aliens :: print_information()
{
cout << this->name << " has ";
cout << this->hp << "hit-points, ";
cout << this->damage << " damage and ";
cout << this->colour << "colour";
}
这里是 main.cpp 的代码:
#include <iostream>
#include "enemy_definitions.h"
using namespace std;
int main()
{
robots Bertha;
Bertha.print_information();
}
有人能找出为什么我总是收到这个错误吗?
您只是在编译 main.cpp 文件,这就是编译器无法 link 您的函数定义的原因。使用
g++ main.cpp enemy_definitions.cpp
这应该 link 您的代码正确。
我正在创建一个程序来练习我在 类 和文件方面的知识,但我可以让它工作。以前我收到一条错误消息说 robots::robots() 被定义了多次,但现在我收到一条错误消息说未定义对 'robots::robots()' 的引用。这是 enemy_definitions.h 的代码:
#include <iostream>
using namespace std;
class enemies
{
public:
string name;
int hp;
int damage;
virtual void print_information();
};
class robots: public enemies
{
public:
robots();
void print_information();
private:
int power_requirement;
};
class zombies: public enemies
{
public:
void print_information();
private:
int height;
};
class aliens: public enemies
{
public:
void print_information();
private:
string colour;
};
这是 enemy_definitions.cpp 的代码:
#include <iostream>
#include "enemy_definitions.h"
void enemies :: print_information()
{
}
robots :: robots()
{
cout <<"Name: ";
cin >> name;
cout <<"\nhp: ";
cin >> hp;
cout <<"\ndamage: ";
cin >> damage;
cout <<"\n power_requirement: ";
cin >> power_requirement;
}
void robots :: print_information()
{
cout << this->name << " has ";
cout << this->hp << "hit-points, ";
cout << this->damage << " damage and ";
cout << this->power_requirement << "power requirement";
}
void zombies :: print_information()
{
cout << this->name << " has ";
cout << this->hp << "hit-points, ";
cout << this->damage << " damage and ";
cout << this->height << "height";
}
void aliens :: print_information()
{
cout << this->name << " has ";
cout << this->hp << "hit-points, ";
cout << this->damage << " damage and ";
cout << this->colour << "colour";
}
这里是 main.cpp 的代码:
#include <iostream>
#include "enemy_definitions.h"
using namespace std;
int main()
{
robots Bertha;
Bertha.print_information();
}
有人能找出为什么我总是收到这个错误吗?
您只是在编译 main.cpp 文件,这就是编译器无法 link 您的函数定义的原因。使用
g++ main.cpp enemy_definitions.cpp
这应该 link 您的代码正确。