为什么此代码会引发 LNK2019 错误?

发布于 2024-12-17 14:12:24 字数 2538 浏览 4 评论 0原文

我只是通过构建这个简单的程序来确保我理解继承,其中狗从哺乳动物继承。我编译时遇到错误。它应该做的就是进入哺乳动物和狗的构造函数,吠叫,然后进入哺乳动物和狗的析构函数。我很抱歉帖子中的缩进有点偏离,它在 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 ==========

I was just making sure I understood inheritance by building this simple program where Dog inherits from Mammal. I am getting an error when I compile it. All it should do is go into a constructor for mammal and dog, bark, then go into a destructor for mammal and dog. I apologize that the indentation came out a bit off in the post, it is well organized in 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;
}

The output of the compiler is as follows:

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 ==========

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

久光 2024-12-24 14:12:24

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

    Dog::~Dog(){}

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

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

工作示例

You declared a destructor for Dog class, but did not define it.
You should define it as well.

    Dog::~Dog(){}

Error on gcc, clearly tells that:

prog.cpp:(.text+0xf54): undefined reference to Dog::~Dog()
prog.cpp:(.text+0xfc3): undefined reference to Dog::~Dog()

Working sample of your code after the modification

若沐 2024-12-24 14:12:24

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

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

You haven't defined the destructor for Dog:

Dog::~Dog()
{
    cout << "Dog destructor." << endl;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文