gpt4 book ai didi

c++ - 链接器错误 - undefined reference

转载 作者:行者123 更新时间:2023-11-28 05:56:42 24 4
gpt4 key购买 nike

我正在创建一个程序,但是当我运行它时出现这些错误:

/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
}

最佳答案

您需要编译所有您的源文件(不是头文件)并将它们链接在一起。此时,最直接的方法是在一个命令中指定所有文件名:

g++ maingame.cpp character.cpp human.cpp orc.cpp

大型项目通常会单独编译每个文件,然后链接生成的.o(目标)文件,但您似乎还没有做到这一点。

关于c++ - 链接器错误 - undefined reference ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34012060/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com