C++ 链接器错误:未定义对vtable的引用
我希望你能帮我解决我的问题,这个节目是一个剧院预订节目 main.cppC++ 链接器错误:未定义对vtable的引用,c++,gcc,linker-errors,C++,Gcc,Linker Errors,我希望你能帮我解决我的问题,这个节目是一个剧院预订节目 main.cpp { #include <cstdlib> #include <iostream> #include "headerMachine.h" using namespace std; int main(int argc, char *argv[]) { Machine machineCall; Movies moviesCall; machineCall.menu();
{
#include <cstdlib>
#include <iostream>
#include "headerMachine.h"
using namespace std;
int main(int argc, char *argv[])
{
Machine machineCall;
Movies moviesCall;
machineCall.menu();
moviesCall.chooseMovie();
system("pause");
return 0;
}
我真的不知道现在该怎么办,我为这个程序工作了大约3个小时。文件
imp.cpp
只是header.h
的一个副本。源文件应包含头文件中方法的实现
像
等等
您遇到的问题是因为您的类中没有构造函数或析构函数。包含虚函数的类还必须实现构造函数和(虚)析构函数。不必包含任何内容并且可以为空,只需定义 错误消息中有一个提示:
[Linker error] main.o:main.cpp:(.text$_ZN7MachineC2Ev[Machine::Machine()]+0x8): undefined reference to `vtable for Machine'
Here -------------------------------------------------^^^^^^^^^^^^^^^^
实现构造函数和析构函数,它应该可以工作
由于构造函数和析构函数可以为空,我建议您将它们添加到头文件中的类定义中:
class Machine
{
public:
Machine() {} // Constructor with empty body
virtual ~Machine() {} // Empty virtual destructor
// Rest of the class...
};
对另一个类执行相同的操作,但当然要使用专有名称。实现缺少的函数。您的
imp.cpp
与您的header.h
完全相同。请了解如何使用虚拟和纯虚拟。基本上是关于继承的。要么让Machine::reserveSeats和Machine::availableSeats纯虚拟,要么为它们提供默认实现。@TomKnapen,我试过你说的,但我不能在我的基类中调用我的函数谢谢你的快速回复!对不起,是我的错,我抄错了,我会改的then@MaryGraceIsananViaje请看我的最新答案我试过你说的,但什么也没发生,还是一样的错误你能给我举个例子吗?只是我不确定我是否正确地实现了构造函数和析构函数,谢谢你的回复,我真的很感激it@MaryGraceIsananViaje查看我的最新答案
[Linker error] main.o:main.cpp:(.text$_ZN7MachineC2Ev[Machine::Machine()]+0x8): undefined reference to `vtable for Machine'
[Linker error] main.o:main.cpp:(.text$_ZN7MachineC1Ev[Machine::Machine()]+0x8): undefined reference to `vtable for Machine'
collect2: ld returned 1 e xit status
C:\Users\Mary Grace\Desktop\Theater Reservation Program\Makefile.win [Error] [TheaterProject.exe] Error 1 (if this is the only error: please check your library includes)
void Machine::menu()
{
// Code for this method
}
[Linker error] main.o:main.cpp:(.text$_ZN7MachineC2Ev[Machine::Machine()]+0x8): undefined reference to `vtable for Machine'
Here -------------------------------------------------^^^^^^^^^^^^^^^^
class Machine
{
public:
Machine() {} // Constructor with empty body
virtual ~Machine() {} // Empty virtual destructor
// Rest of the class...
};