我一直收到错误未定义的引用'vtable for Producer' 为我的子类生产者:
#include "Producer.h"
Producer::Producer (unsigned int ID, int age1, std::string name1,
char gender1, std::string jobDescription1)
{IDNumber = ID; age = age1; name = name1;
jobDescription = jobDescription1; gender = gender1;}
void SetJobDescription(std::string description){}
void PrintPersonnel(){cout<<"";}
Producer::~Producer(){}
父母是:
#include <string>
#include <iostream>
class Personnel{
protected:
unsigned int IDNumber;
int age;
std::string jobDescription;
std::string name;
char gender;
public:
virtual void PrintPersonnel() = 0;
unsigned int GetID();
int GetAge();
std::string Getname();
std::string GetjobDescription();
char GetGender();
virtual ~Personnel();
};
// Personel.cpp
#include "Personnel.h"
unsigned int Personnel::GetID(){return IDNumber;}
int Personnel::GetAge(){return age;}
std::string Personnel::Getname(){return name;}
std::string Personnel::GetjobDescription(){return jobDescription;}
char Personnel::GetGender(){return gender;}
Personnel::~Personnel(){}
它具有纯虚函数。 为什么我不能实现子类Producer?
非常感谢。
答案 0 :(得分:2)
代码中的问题是您忘记了一个成员函数的范围。试试:
void Producer::PrintPersonnel(){cout<<"";} // Producer:: was missing
// so you were defining a global function instead
// of a member function.
请注意,您对SetJobDescription()
的定义存在类似问题。由于它不是虚函数,它只会引导您进入未定义的引用。
如果它仍然不起作用,那么您应该在评论中建议的某个地方定义~Personnel()
。