链接器错误:未定义的vtable引用[复制]

时间:2022-11-25 05:33:07

This question already has an answer here:

这个问题已经有了答案:

I'm hoping that you can help me with my problem, this program is a theater reservation program.

我希望你能帮我解决我的问题,这个项目是一个剧场预订计划。

main.cpp

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

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

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<<"\n\nChoose 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<<"\n\n 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;
        }
}

Errors:

错误:

[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) 

I really don't know what to do now, I've been working for this prog for about 3 hours.

我真的不知道该怎么办了,我已经为这个项目工作了3个小时了。

1 个解决方案

#1


1  

The file imp.cpp is just a copy of header.h. The source file should contain the implementation of the methods in the header file.

文件impl .cpp只是header.h的副本。源文件应该包含在头文件中的方法的实现。

Like

就像

void Machine::menu()
{
    // Code for this method
}

Etc.

等。


The problems you are having is because you don't have a construct or destructor in your classes. A class containing virtual function must also implement a constructor and a (virtual) destructor. The don't have to contain anything and can be empty, the just have to be defined.

您所遇到的问题是您的类中没有构造或析构函数。包含虚拟函数的类还必须实现构造函数和(虚拟)析构函数。不需要包含任何东西,也可以是空的,只需要定义。

One hint is in the error message:

在错误信息中有一个提示:

[Linker error] main.o:main.cpp:(.text$_ZN7MachineC2Ev[Machine::Machine()]+0x8): undefined reference to `vtable for Machine'

Here -------------------------------------------------^^^^^^^^^^^^^^^^

Implement the constructors and destructors and it should work.

实现构造函数和析构函数,它应该工作。

As the constructors and destructors can be empty, I recommend you just add them to the class definition in the header file:

由于构造函数和析构函数可能是空的,所以我建议您将它们添加到头文件中的类定义中:

class Machine
    {
        public:
            Machine() {}  // Constructor with empty body
            virtual ~Machine() {}  // Empty virtual destructor

            // Rest of the class...
    };

Do the same for the other class, but of course with the proper names.

对其他类也做同样的事情,但是当然要用正确的名字。

#1


1  

The file imp.cpp is just a copy of header.h. The source file should contain the implementation of the methods in the header file.

文件impl .cpp只是header.h的副本。源文件应该包含在头文件中的方法的实现。

Like

就像

void Machine::menu()
{
    // Code for this method
}

Etc.

等。


The problems you are having is because you don't have a construct or destructor in your classes. A class containing virtual function must also implement a constructor and a (virtual) destructor. The don't have to contain anything and can be empty, the just have to be defined.

您所遇到的问题是您的类中没有构造或析构函数。包含虚拟函数的类还必须实现构造函数和(虚拟)析构函数。不需要包含任何东西,也可以是空的,只需要定义。

One hint is in the error message:

在错误信息中有一个提示:

[Linker error] main.o:main.cpp:(.text$_ZN7MachineC2Ev[Machine::Machine()]+0x8): undefined reference to `vtable for Machine'

Here -------------------------------------------------^^^^^^^^^^^^^^^^

Implement the constructors and destructors and it should work.

实现构造函数和析构函数,它应该工作。

As the constructors and destructors can be empty, I recommend you just add them to the class definition in the header file:

由于构造函数和析构函数可能是空的,所以我建议您将它们添加到头文件中的类定义中:

class Machine
    {
        public:
            Machine() {}  // Constructor with empty body
            virtual ~Machine() {}  // Empty virtual destructor

            // Rest of the class...
    };

Do the same for the other class, but of course with the proper names.

对其他类也做同样的事情,但是当然要用正确的名字。