求纠错,实例化后无法编译,不晓得错在哪
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未定义,之前的课程未涉及纯虚函数时是正常的。
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()这个函数啊
求纠错
ps:编译时就报错了,还没到运行的步骤