【问题标题】:Why does this code raise a LNK2019 error?为什么此代码会引发 LNK2019 错误?
【发布时间】:2011-11-22 15:32:30
【问题描述】:

我只是通过构建这个 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 ==========

【问题讨论】:

  • 请发布您收到的完整错误消息。
  • 也许你可以发布它给你的错误;)?
  • 刚刚发布了编译器的完整日志,对此感到抱歉。

标签: c++ inheritance compiler-errors lnk


【解决方案1】:

您为 Dog 类声明了一个析构函数,但没有定义它。
你也应该定义它。

    Dog::~Dog(){}

gcc 上的错误,清楚地表明:

prog.cpp:(.text+0xf54): 未定义对 Dog::~Dog() 的引用
prog.cpp:(.text+0xfc3): 未定义对 Dog::~Dog() 的引用

Working sample你修改后的代码

【讨论】:

    【解决方案2】:

    你还没有为Dog定义析构函数:

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

    【讨论】:

      猜你喜欢
      • 2020-03-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多