我正在创建一个程序,但是当我运行它时,我收到了这些错误:
/tmp/ccYLF4IM.o: In function `main':
maingame.cpp:(.text+0x13): undefined reference to `Human::Human()'
maingame.cpp:(.text+0x1f): undefined reference to `Orc::Orc()'
maingame.cpp:(.text+0x99): undefined reference to `Orc::~Orc()'
maingame.cpp:(.text+0xac): undefined reference to `Orc::~Orc()'
maingame.cpp:(.text+0xc5): undefined reference to `Human::~Human()'
maingame.cpp:(.text+0xdf): undefined reference to `Human::~Human()'
collect2: ld returned 1 exit status
我查看了我的代码,我正在运行所有的.cpp文件和.h文件。我不确定问题是什么。我知道还有其他问题,但我似乎无法找到我想要的答案。我已检查过所有内容的大小写,我甚至删除了所有内容并重新开始以确保。任何有关这方面的帮助将不胜感激。谢谢
//main.cpp
#include "Human.h"
#include "Orc.h"
using namespace std;
//Main.cpp
int main()
{
//Character cc;
Human H;
Orc O;
char choice;
char userC;
cout << "Welcome!\n";
cout << "" << endl;
cout << "Pick your choice:\n";
cout << "A -- Human\n";
cout << "B -- Orc\n";
cin >> choice;
return 0;
}
//Character.h
#ifndef CHARACTER_H
#define CHARACTER_H
#include <iostream>
#include <string>
using namespace std;
class Character
{
protected:
float characterTotal;
public:
virtual int createCharacter() = 0; //Pure virtual function
Character();
~Character();
};
#endif
//Character.cpp
#include "Character.h"
Character::Character()
{
//ctor
}
Character::~Character()
{
//dtor
}
//human.h
#ifndef HUMAN_H
#define HUMAN_H
#include "Character.h"
using namespace std;
class Human : public Character
{
private:
int characterStrength;
int characterDexterity;
int characterIntelligence;
string characterType;
int characterTotal;
public:
Human();//Constructor
~Human();
int getStrength ()
{
cout << "Enter a number from 0 to 18\n";
cin >> characterStrength;
return characterStrength;
}
int getDexterity ()
{
cout << "Enter a number from 0 to 18\n";
cin >> characterDexterity;
return characterDexterity;
}
int getIntelligence ()
{
cout << "Enter a number from 0 to 18\n";
cin >> characterIntelligence;
return characterIntelligence;
}
string getType ()
{
cout << "Please choose one of the following\n";
cout << "A -- Paladin \n";
cout << "B -- Ranger \n";
cout << "C -- Wizard \n";\
cin >> characterType;
return characterType;
}
virtual int createCharacter()
{
characterTotal = characterStrength + characterIntelligence + characterDexterity;
return characterTotal;
}
};
#endif
//Human.cpp
#include "Human.h"
Human::Human()
{
//ctor
}
Human::~Human()
{
//dtor
}
//orc.h
#ifndef ORC_H
#define ORC_H
#include "Character.h"
using namespace std;
class Orc : public Character
{
private:
int characterStrength;
int characterDexterity;
int characterIntelligence;
int chaD;
int chaI;
int chaS;
string characterClan;
int characterTotal;
public:
Orc(); //Constructor
~Orc();
int getStrength()
{
cout << "Enter a number between 0 to 18\n";
cin >> chaS;
characterStrength = chaS + 2;
return characterStrength;
}
int getDexterity()
{
cout << "Enter a number between 0 to 18\n";
cin >> chaD;
characterDexterity = chaD - 2;
return characterDexterity;
}
int getIntelligence()
{
cout << "Enter a number between 0 to 18\n";
cin >> chaI;
characterIntelligence = chaI - 2;
return characterIntelligence;
}
string getClan()
{
cout << "Please choose one of the following\n";
cout << "A -- Barbarian \n";
cout << "B -- Berserker \n";
cout << "C -- Vanguard \n";\
cin >> characterClan;
return characterClan;
}
virtual int createCharacter()
{
characterTotal = characterStrength + characterIntelligence + characterDexterity;
return characterTotal;
}
};
#endif
//Orc.cpp
#include "Orc.h"
Orc::Orc()
{
//ctor
}
Orc::~Orc()
{
//dtor
}
答案 0 :(得分:1)
您需要编译所有源文件(不是头文件)并将它们链接在一起。此时,最简单的方法是在一个命令中指定所有文件名:
g++ maingame.cpp character.cpp human.cpp orc.cpp
大型项目通常单独编译每个文件,然后链接生成的.o
(对象)文件,但您似乎还没有达到这一点。
答案 1 :(得分:0)
immibis有一个观点,但我认为你应该花一些时间来研究makefile。因为看起来你正在构建一个安静的项目,所以每次手动编译都很不舒服。 This教程是对makefile的一个很好的介绍。永远不要停止学习!