求糾錯,實例化后無法編譯,不曉得錯在哪
Person.h
#ifndef?PEERSON_H #define?PEERSON_H #include?<string> using?namespace?std; class?Person{ public: Person(string?n); virtual?~Person(){} virtual?void?work()=0; protected: string?Name; }; #endif
Person.cpp
#include?"Person.h" Person::Person(string?n){ ????Name=n; }
Worker.h
#ifndef?WORKER_H #define?WORKER_H #include?"Person.h" class?Worker:public?Person{ public: Worker(string?n,int?a); virtual?void?work(); protected: int?Age; }; #endif
Worker.cpp
#include?"Worker.h" #include?<iostream> Worker::Worker(string?n,int?a):Person(n){ ????Age=a; } void?work(){ ????//cout<<Name<<"?is?"<<Age<<endl; ????cout<<"work--worker"<<endl; }
這個地方就不能用cout<<Name<<"?is?"<<Age<<endl,編譯說Name和Age未定義,之前的課程未涉及純虛函數(shù)時是正常的。
Dustman.h
#include?"Worker.h" class?Dustman:public?Worker{ public: Dustman(string?n,int?a); virtual?~Dustman(){} void?work(); };
Dustman.cpp
#include?"Dustman.h" #include?<iostream> Dustman::Dustman(string?n,int?a):Worker(n,a){ } void?Dustman::work(){ cout<<Name<<"?is?"<<Age<<"?--Dustman"<<endl; }
這里編譯又正常了,可以用Name和Age,求教上面為什么不能用
demo.cpp
#include?"Dustman.h" int?main(){ /*Dustman?dd("merry",18); dd.work();*/ Person?*p?=?new?Dustman("merry",18); p->work(); delete?p; p=NULL; return?0; }
用2種實例化都不行,報錯一樣的
/tmp/cc8z9iC2.o: In function `Worker::~Worker()':
Dustman.cpp:(.text._ZN6WorkerD2Ev[_ZN6WorkerD5Ev]+0x7): undefined reference to `vtable for Worker'
/tmp/cc8z9iC2.o:(.rodata._ZTI7Dustman[_ZTI7Dustman]+0x8): undefined reference to `typeinfo for Worker'
/tmp/ccEPTJOC.o: In function `Worker::Worker(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, int)':
Worker.cpp:(.text+0x53): undefined reference to `vtable for Worker'
啥意思?我沒定義~Worker()這個函數(shù)啊
求糾錯
ps:編譯時就報錯了,還沒到運行的步驟
2016-11-01
#include?"Worker.h"
#include?<iostream>
?
Worker::Worker(string?n,int?a):Person(n){
????Age=a;
}
void ?Worker::work(){
????//cout<<Name<<"?is?"<<Age<<endl;
????cout<<"work--worker"<<endl;
}
少加了下劃線的地方