为什么此代码会引发 LNK2019 错误?
我只是通过构建这个简单的程序来确保我理解继承,其中狗从哺乳动物继承。我编译时遇到错误。它应该做的就是进入哺乳动物和狗的构造函数,吠叫,然后进入哺乳动物和狗的析构函数。我很抱歉帖子中的缩进有点偏离,它在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您为
Dog
类声明了析构函数,但没有定义它。您也应该定义它。
gcc 上的错误,清楚地表明:
工作示例
You declared a destructor for
Dog
class, but did not define it.You should define it as well.
Error on gcc, clearly tells that:
Working sample of your code after the modification
您还没有为
Dog
定义析构函数:You haven't defined the destructor for
Dog
: