为什么我总是收到错误:"未定义对'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();
}
有人可以发现为什么我不断收到此错误。
你只是在编译你的主文件.cpp这就是为什么编译器无法链接你的函数定义。用
g++ main.cpp enemy_definitions.cpp
这应该正确链接您的代码。
相关文章:
- 将对象数组的引用传递给函数
- 什么时候在C++中返回常量引用是个好主意
- 我想将一个对T类型的非常量左值引用绑定到一个T类型的临时值
- 何时在引用或唯一指针上使用移动语义
- 如何在c++中使用引用实现类似python的行为
- 编译C++时未定义的引用
- Ctypes wstring通过引用传递
- c++r值引用应用于函数指针
- 理解c++中的引用
- C++取消引用指针.为什么会发生变化
- 如何修复此错误:未定义对"距离(浮点数,浮点数,浮点数,浮点数,浮点数)"的引用
- 我的项目不会像"undefined reference to `grpc::g_core_codegen_interface'"那样使用未定义的引用错误进行编译
- C++Boost Asio Pool线程,带有lambda函数和传递引用变量
- 强制转换为引用类型
- 引用一个已擦除类型(void*)的指针
- 向量元素的引用地址与它所指向的向量元素的地址不同.为什么
- 具有默认值的引用获取函数
- 如何使用基类指针引用派生类成员
- 使用取消引用的指针的多态性会产生意外的结果.为什么?
- 为什么我总是收到错误:"未定义对'robots::robots()'的引用