C++ 键入对"生产者的 vtable"的未定义引用

C++ Type undefined reference to `vtable for Producer'

本文关键字:vtable 未定义 引用 生产者 C++      更新时间:2023-10-16

我一直收到错误,未定义引用`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?

非常感谢。

代码中的问题是您忘记了一个成员函数的作用域。尝试:

void Producer::PrintPersonnel(){cout<<"";} // Producer:: was missing
                                           // so you were defining a global function instead
                                           // of a member function. 

请注意,您对SetJobDescription()的定义也有类似的问题。由于它不是一个虚拟函数,它只会将您引向一个未定义的引用。

如果它仍然不起作用,那么您应该将某个地方~Personnel()定义为注释中建议的某个人。