编译代码时出错

时间:2015-11-30 17:56:30

标签: c++ compilation linker linker-errors

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

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

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
}

1 个答案:

答案 0 :(得分:1)

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