//Person類的定義
#ifndef?PERSON_H_
#define?PERSON_H_
using?namespace?std;
#include?<string>
class?Person{
public:
????Person(string?name);
????virtual?~Person();
????virtual?void?work()?=?0;
protected:
????string?m_strName;
};
#endif//Person類的實(shí)現(xiàn)
#include?"Person.h"
#include?<iostream>
Person::Person(string?name)
{
????m_strName?=?name;
????cout?<<?"Person()"?<<?endl;
}
Person::~Person()
{
????cout?<<?"~Person"?<<?endl;
}//Worker類的定義
#ifndef?WORKER_H_
#define?WORKER_H_
#include?"Person.h"
class?Worker?:public?Person
{
public:
????Worker(string?name,int?age);
????virtual?~Worker();
????//virtual?void?work();
protected:
????int?m_iAge;
};
#endif//Worker類的實(shí)現(xiàn)
#include"Worker.h"
#include?<iostream>
Worker::Worker(string?name,int?age)?:Person(name)
{
????m_iAge?=?age;
????cout?<<?"Worker()"?<<?endl;
}
Worker::~Worker()
{
????cout?<<?"~Worker"?<<?endl;
}//Dustman類的定義
#ifndef?DUSTMAN_H_
#define?DUSTMAN_H_
#include?"Worker.h"
class?Dustman?:public?Worker
{
public:
????Dustman(string?name,int?age);
????~Dustman();
????virtual?void?work();
};
#endif//Dustman類的實(shí)現(xiàn)
#include?"Dustman.h"
#include?<iostream>
Dustman::Dustman(string?name,?int?age)?:Worker(name,age)
{
????cout?<<?"Dustman()"?<<?endl;
}
Dustman::~Dustman()
{
????cout?<<?"~Dustman"?<<?endl;
}
void?Dustman::work()
{
????cout?<<?"work()"?<<?endl;
}//main函數(shù)
#include?<iostream>
#include?"Dustman.h"
#include?<stdlib.h>
using?namespace?std;
int?main(void){
????Dustman?persn("jin",20);
????system("pause");
????return?0;
}編譯可以通過(guò),但運(yùn)行就會(huì)報(bào)錯(cuò),下面是所有的錯(cuò)誤提示,點(diǎn)擊錯(cuò)誤提示不能提示是哪一行出錯(cuò)了,所以搞不明白哪兒錯(cuò)了警告?? ?1?? ?warning LNK4042: 對(duì)象被多次指定;已忽略多余的指定?? ?E:\visual studio 2013\Projects\test\test\Debug\Dustman.obj?? ?1?? ?1?? ?test警告?? ?2?? ?warning LNK4042: 對(duì)象被多次指定;已忽略多余的指定?? ?E:\visual studio 2013\Projects\test\test\Debug\Person.obj?? ?1?? ?1?? ?test錯(cuò)誤?? ?3?? ?error LNK2019: 無(wú)法解析的外部符號(hào) "public: __thiscall Dustman::Dustman(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,int)" (??0Dustman@@QAE@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@H@Z),該符號(hào)在函數(shù) _main 中被引用?? ?E:\visual studio 2013\Projects\test\test\demo.obj?? ?test錯(cuò)誤?? ?4?? ?error LNK2019: 無(wú)法解析的外部符號(hào) "public: virtual __thiscall Dustman::~Dustman(void)" (??1Dustman@@UAE@XZ),該符號(hào)在函數(shù) _main 中被引用?? ?E:\visual studio 2013\Projects\test\test\demo.obj?? ?test錯(cuò)誤?? ?5?? ?error LNK1120: 2 個(gè)無(wú)法解析的外部命令?? ?E:\visual studio 2013\Projects\test\Debug\test.exe?? ?test
C++多態(tài)篇課上的一個(gè)例子運(yùn)行出錯(cuò),關(guān)于抽象類的
BlueCitizen
2015-12-06 12:13:56