我只是確保通過構建這個簡單的程序來理解繼承,這個程序是Dog繼承了哺乳動物。編譯時出現錯誤。所有它應該做的是進入一個哺乳動物和狗的構造函數,樹皮,然後進入哺乳動物和狗的析構函數。我很抱歉縮進在帖子中出現了一點,它在Visual Studio中組織良好。這段代碼爲什麼會引發LNK2019錯誤?
#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)" ([email protected]@[email protected]) 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 ==========
請張貼完整的錯誤消息你得到。 –
也許你可以發佈它給你的錯誤;)? – Smash
剛剛發佈了來自編譯器的完整日誌,對此抱歉。 – SaxSalute