链接器错误:对虚函数表的未定义引用
Linker error: undefined reference to vtable
我希望你能帮我解决这个问题,这个程序是一个剧院预订程序。
main.cpp
{
#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;
}
header.h
#include <string>
#include <iostream>
using namespace std;
class Machine
{
public:
void menu(); //
virtual void reserveSeats();
virtual void availableSeats();
void ticketSales();
private:
int task;
};
class Movies: public Machine
{
public:
void chooseMovie(); //
void setTitle();
void timeSlot();
void receipt();
//Inheritance
void reserveSeats();//
void availableSeats();
protected:
int chosenMovie;
int rowNum;
int seatNum;
int seatsThHo[10][10];
int seatsROG[10][10];
int seatsHoTr[10][10];
};
imp.cpp
#include <cstdlib>
#include <iostream>
#include "headerMachine.h"
using namespace std;
const char RESERVED = '*';
const char VACANT = 'o';
void Machine::menu()
{
cout<<"n [1]Reserve Seats ";
cout<<"n [2]Available Seats ";
cout<<"n [3]View Ticket Sales ";
cout<<"n [0]Exit ";
cout<<"nnChoose task number: ";
cin>>task;
}
void Movies::chooseMovie()
{
cout<<"n [1]The Hobbit ";
cout<<"n [2]Rise of the Guardians ";
cout<<"n [3]Hotel Transylvania ";
cout<<"n [0]Back ";
cout<<"nn From what movie: ";
cin>>chosenMovie;
}
void Movies::reserveSeats()
{
cout<<"nEnter row #: ";
cin>>rowNum;
cout<<"nEnter seat#: ";
cin>>seatNum;
for(int i=0; i<10; i++)
{
for(int j=0; j<10; j++)
{
seatsThHo[i][j] = VACANT;
seatsROG[i][j] = VACANT;
seatsHoTr[i][j] = VACANT;
}
}
switch(chosenMovie)
{
case 1:
{
cout<<"nYou wanted to buy ticket from the movie 'The Hobbit': ";
cout<<"nYour row # is "<<rowNum<<" and you choosed seat # "<<seatNum;
seatsThHo[rowNum][seatNum] = RESERVED;
}
break;
case 2:
{
cout<<"nYou wanted to buy ticket from the movie 'Rise Of The Guardians': ";
cout<<"nYour row # is "<<rowNum<<" and you choosed seat # "<<seatNum;
seatsROG[rowNum][seatNum] = RESERVED;
}
break;
case 3:
{
cout<<"nYou wanted to buy ticket from the movie 'Hotel Transylvania': ";
cout<<"nYour row # is "<<rowNum<<" and you choosed seat # "<<seatNum;
seatsHoTr[rowNum][seatNum] = RESERVED;
}
break;
case 0:
break;
}
}
void Movies::availableSeats()
{
switch(chosenMovie)
{
case 1:
{
cout<<"nAvailable Seats for the movie 'Rise Of The Guardians': ";
for(int x=0; x<10; x++)
{
for(int y=0; y<10; y++)
{
cout<<seatsThHo[x][y]<<" ";
}
}
}
break;
case 2:
{
cout<<"nAvailable Seats for the movie 'Rise Of The Guardians': ";
for(int x=0; x<10; x++)
{
for(int y=0; y<10; y++)
{
cout<<seatsROG[x][y]<<" ";
}
}
}
break;
case 3:
{
cout<<"nAvailable Seats for the movie 'Hotel Transylvania': ";
for(int x=0; x<10; x++)
{
for(int y=0; y<10; y++)
{
cout<<seatsHoTr[x][y]<<" ";
}
}
}
break;
case 0:
break;
}
}
错误:
[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:UsersMary GraceDesktopTheater Reservation ProgramMakefile.win [Error] [TheaterProject.exe] Error 1 (if this is the only error: please check your library includes)
我真的不知道现在该做什么,我已经为这个项目工作了大约3个小时了
文件imp.cpp
只是header.h
的副本。源文件应该包含头文件中方法的实现。
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...
};
对另一个类执行相同的操作,但当然要使用适当的名称。
相关文章:
- 对C宏的未定义引用,但在定义它时会出现重新定义错误
- 编译时的 CImg 库返回对"__imp_SetDIBitsToDevice"的未定义引用
- 对Py_Initialize()的未定义引用
- 使用mysql c++连接器的未定义引用
- 对 Scalar ::Scalar() 的未定义引用
- 对复制 CTOR 和 CTOR 的未定义引用
- 对显式实例化的模板函数的未定义引用
- TensorRT (C++ API) 对"createNvOnnxParser_INTERNAL"的未定义引用
- 2个模板化类的非模板友元函数未定义引用错误
- 编译 libfluid 样本控制器时对"event_base_del_virtual"的未定义引用
- 获取对function_name的未定义引用
- 对 'std::thread::_M_start_thread CMake 的未定义引用进行基准测试
- 对结构方法的未定义引用
- 使用内联函数 c++ 的未定义引用
- 对 CMake 中'cudaRegisterLinkedBinary'链接错误的未定义引用?
- 对 DLOPEN 的未定义引用
- QT C++中对全局变量的未定义引用
- 快速数学导致对"__pow_finite"的未定义引用
- 对 boost::system::d etail::system_category_instance 的未定义引用,从
- OpenCV 3.4.3 中对 'cv::String::d eallocate()' 错误的未定义引用