【问题标题】:error when compiling code [duplicate]编译代码时出错[重复]
【发布时间】:2015-11-30 17:56:30
【问题描述】:

我似乎无法找出此错误的原因。我经历了很多次,都找不到问题所在。这是我第一次看到这个错误。

这是我编译时得到的... 错误:

game.cpp:(.text+0x13): undefined reference to `Human::Human()'
game.cpp:(.text+0x1f): undefined reference to `orc::orc()'
collect2: ld returned 1 exit status

#include <iostream>  
#include "Character.h"
#include "Human.h"
#include "Orc.h"

using namespace std;

//Main.cpp
int main()
{
    //Character cc;
    Human hh;
    orc oo;
    char choice;
    char userC;
    cout << "Welcome!\n";
    cout << "" << endl;

    cout << "Pick your choice:\n";
    cout << "A -- Human\n";
    cout << "B -- Orc\n";
    cin >> choice;

    switch (choice)
    {
        case 'a':
        case 'A': 
                    hh.getStrength();
                    hh.getDexterity();
                    hh.getIntelligence();
                    hh.getType();
                    hh.createCharacter();

                    //cout << ""












        //case 'b':
        //case 'B':









    }

    return 0;
}

//character.h
#ifndef CHARACTER_H
#define CHARACTER_H

using namespace std;

class Character
{
    protected:
                float characterTotal;

    public:
                virtual int createCharacter() = 0; //Pure virtual function


};

#endif

//human.h
#ifndef HUMAN_H
#define HUMAN_H

#include "Character.h"
using namespace std;

class Human
{
    private:
                int characterStrength;
                int characterDexterity;
                int characterIntelligence;
                string characterType;
                int characterTotal;

    public:
                Human();//Constructor 

                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

-----------------------------------------------------------------------

//orc.h
#ifndef ORC_H
#define ORC_H

#include "Character.h"
#include "Human.h"

using namespace std;

class orc
{
    private:
                int characterStrength;
                int characterDexterity;
                int characterIntelligence;
                int chaD;
                int chaI;
                int chaS;
                string characterClan;
                int characterTotal;

    public:
                orc(); //Constructor

                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

//Human.cpp
#include "Human.h"
Human::Human()
{
//ctor
}
Human::~Human()
{
//dtor
}

//Orc.cpp
#include "Orc.h"
Orc::Orc()
{
//ctor
}
Orc::~Orc()
{
//dtor
}

//Character.cpp
#include "Character.h"
Character::Character()
{
//ctor
}
Character::~Character()
{
//dtor
}

【问题讨论】:

  • 什么是Human?你不应该在某个地方做一个实例吗?
  • @SergeyA 为什么要删除 c++ 标签??那是一个无效的编辑,作为一个值得信赖的编辑,你应该更清楚!
  • @πάνταῥεῖ,可能是偶然的。对于那个很抱歉。它仍然与多态性无关,并且会从链接器中受益。
  • @otto 做更多研究,彻底阅读副本。这个问题很琐碎,之前被问过一百万次!
  • @otto 您还应该显示您的实际链接器命令行。您是否包含所有目标文件以链接到您的最终目标?

标签: c++ compilation linker linker-errors


【解决方案1】:

链接代码时,链接行应包含与程序相关的所有源文件的 .o 或 .cpp 文件。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-01-22
    • 1970-01-01
    • 1970-01-01
    • 2023-03-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多