Почему я получаю сообщение об ошибке: «неопределенная ссылка на« robots :: robots () » - PullRequest
0 голосов
/ 05 июля 2018

Я создаю программу, чтобы практиковать свои знания в классах и файлах, но я могу заставить ее работать. Ранее я получал сообщение о том, что robots :: robots () был определен несколько раз, но теперь у меня есть эта ошибка, говорящая о неопределенной ссылке на 'robots :: robots ()'. Вот код для врага_definitions.h:

#include <iostream>
using namespace std;

class enemies
{
    public:
    string name;
    int hp;
    int damage;
    virtual void print_information();
};

class robots: public enemies
{
    public:
        robots();
        void print_information();
    private:
        int power_requirement;
};

class zombies: public enemies
{
    public:
        void print_information();
    private:
        int height;
};

class aliens: public enemies
{
    public:
        void print_information();
    private:
        string colour;
};

Вот код для врага_definitions.cpp:

#include <iostream>
#include "enemy_definitions.h"

void enemies :: print_information()
{
}

robots :: robots()
    {
        cout <<"Name: ";
        cin >> name;
        cout <<"\nhp: ";
        cin >> hp;
        cout <<"\ndamage: ";
        cin >> damage;
        cout <<"\n power_requirement: ";
        cin >> power_requirement;
    }

void robots :: print_information()
{
    cout << this->name << " has ";
    cout << this->hp << "hit-points, ";
    cout << this->damage << " damage and ";
    cout << this->power_requirement << "power requirement";
}

void zombies :: print_information()
{
    cout << this->name << " has ";
    cout << this->hp << "hit-points, ";
    cout << this->damage << " damage and ";
    cout << this->height << "height";
}

void aliens :: print_information()
{
    cout << this->name << " has ";
    cout << this->hp << "hit-points, ";
    cout << this->damage << " damage and ";
    cout << this->colour << "colour";
}

и вот код для main.cpp:

#include <iostream>
#include "enemy_definitions.h"

using namespace std;

int main()
{
robots Bertha;
Bertha.print_information();
}

Может кто-нибудь определить, почему я продолжаю получать эту ошибку.

1 Ответ

0 голосов
/ 05 июля 2018

Вы компилируете только свой файл main.cpp, поэтому компилятор не может связать ваше определение функций. Используйте

g++ main.cpp enemy_definitions.cpp

Это должно правильно связать ваш код.

...