链接器错误 - 未定义的引用
Linker error - undefined reference
我正在创建一个程序,但是当我 运行 它时,我收到了这些错误:
/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
}
您需要编译所有您的源文件(不是头文件)并将它们link在一起。此时,最直接的方法是在一个命令中指定所有文件名:
g++ maingame.cpp character.cpp human.cpp orc.cpp
大型项目通常会单独编译每个文件,然后 link 生成 .o
(目标)文件,但您似乎还没有达到这一点。
immibis 说得有道理,但我认为您应该花些时间研究一下 makefile。因为看起来你正在构建一个安静的项目并且每次都手动编译它是不舒服的。 This 教程很好地介绍了 makefile。永远不要停止学习!
我正在创建一个程序,但是当我 运行 它时,我收到了这些错误:
/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
}
您需要编译所有您的源文件(不是头文件)并将它们link在一起。此时,最直接的方法是在一个命令中指定所有文件名:
g++ maingame.cpp character.cpp human.cpp orc.cpp
大型项目通常会单独编译每个文件,然后 link 生成 .o
(目标)文件,但您似乎还没有达到这一点。
immibis 说得有道理,但我认为您应该花些时间研究一下 makefile。因为看起来你正在构建一个安静的项目并且每次都手动编译它是不舒服的。 This 教程很好地介绍了 makefile。永远不要停止学习!