Почему этот код вызывает ошибку LNK2019? - PullRequest
0 голосов
/ 22 ноября 2011

Я просто убедился, что понимаю наследование, создав эту простую программу, где Dog наследует от Mammal.Я получаю сообщение об ошибке при компиляции.Все, что нужно сделать, - это перейти в конструктор млекопитающих и собак, лаять, а затем перейти в деструктор млекопитающих и собак.Я прошу прощения за то, что отступы немного посторонние в посте, он хорошо организован в Visual Studio.

#include <iostream>
#include <string>
using namespace std;
class Mammal
{
public:
    Mammal();
    Mammal(int age);
    Mammal(int age, int mammal);
    ~Mammal();
    int getAge() {return itsAge;};
    int getWeight() {return itsWeight;};
    void setAge(int x) {itsAge = x;};
    void setWeight(int x) {itsWeight = x;};
    void speak() {cout << "MAMMALALALALALLLL!" << endl;};

private:
    int itsAge, itsWeight;
};

class Dog : public Mammal
{
    public:
        Dog();
        Dog(int age);
        Dog(int age, int weight);
        Dog(int age, int weight, string breed);
        ~Dog();
        void setBreed(string breed) {itsBreed = breed;};
        string getBreed() {return itsBreed;};
        void bark() {cout << "Bark!" << endl;};
    private:
        string itsBreed;
};

Mammal::Mammal()
{
    cout << "Mammal constructor." << endl;
    setAge(0);
    setWeight(0);
}

Mammal::Mammal(int age)
{
    cout << "Mammel(int) constructor." << endl;
    setAge(age);
    setWeight(0);
}

Mammal::Mammal(int age, int weight)
{
    cout << "Mammal(int, int) constructor." << endl;
    setAge(age);
    setWeight(weight);
}

Mammal::~Mammal()
{
    cout << "Mammal deconstructor." << endl;
}

Dog::Dog():
Mammal()
{
    cout << "Dog constructor." << endl;
    setBreed("");
}

Dog::Dog(int age):
Mammal(age)
{
    cout << "Dog(int) constructor." << endl;
    setBreed("");
}

Dog::Dog(int age, int weight):
Mammal(age, weight)
{
    cout << "Dog(int, int) constructor." << endl;
    setBreed("");
}

Dog::Dog(int age, int weight, string breed):
Mammal(age, weight)
{
    cout << "Dog(int, int, string) constructor." << endl;
    setBreed(breed);
}


int main()
{
    Dog Goldie(5, 50, "Lab");
    Goldie.bark();
    system("PAUSE");
    return 0;
}

Вывод компилятора выглядит следующим образом:

1>ClCompile:
1>  main.cpp
1>main.obj : error LNK2019: unresolved external symbol "public: __thiscall Dog::~Dog(void)" (??1Dog@@QAE@XZ) referenced in function _main
1>c:\users\austin\documents\visual studio 2010\Projects\Inheritance\Debug\Inheritance.exe : fatal error LNK1120: 1 unresolved externals
1>
1>Build FAILED.
1>
1>Time Elapsed 00:00:00.31
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

Ответы [ 2 ]

5 голосов
/ 22 ноября 2011

Вы объявили деструктор для класса Dog, но не определили его.
Вы должны также определить его.

    Dog::~Dog(){}

Ошибка в gcc, ясно говорит о том, что:

prog.cpp :(. Text + 0xf54): неопределенная ссылка на Dog :: ~ Dog ()
prog.cpp :(. Text + 0xfc3): неопределенная ссылка на Dog :: ~ Dog ()

Рабочий образец вашего кода после модификации

3 голосов
/ 22 ноября 2011

Вы не определили деструктор для Dog:

Dog::~Dog()
{
    cout << "Dog destructor." << endl;
}
...