Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Vtable issue and moc



  • I'm getting this error when I try to run my code:

    editor.cpp:(.text._ZN10MainWindowD2Ev[_ZN10MainWindowD5Ev]+0x3): referencia a `vtable for MainWindow' sin definir

    This is my code:

    @
    #include <QApplication>
    #include <QMainWindow>
    #include <QLabel>
    #include <QWidget>
    #include <QPushButton>
    #include <QVBoxLayout>
    #include <QMenu>
    #include <QMenuBar>
    #include <QFileDialog>

    class MainWindow : public QMainWindow
    {
    Q_OBJECT
    public:
    MainWindow(): QMainWindow(0)
    {

        // File menu
        QMenu *file;
        file = this->menuBar()->addMenu("&File");
    
        QAction* open = new QAction("&Open", this);
        connect(open, SIGNAL(triggered()), this, SLOT(openFile&#40;&#41;));
        file->addAction(open);
    
        QAction* save = new QAction("&Save", this);
        // connect(open, SIGNAL(triggered()), qApp, SLOT(quit()));
        file->addAction(save);
    
        QAction* saveAs = new QAction("&Save As", this);
        // connect(open, SIGNAL(triggered()), qApp, SLOT(quit()));
        file->addAction(saveAs);
    
        file->addSeparator();
    
        QAction* quit = new QAction("&Quit", this);
        connect(quit, SIGNAL(triggered()), qApp, SLOT(quit()));
        quit->setShortcut(tr("CTRL+Q"));
        file->addAction(quit);
    
        // View menu
        QMenu *view;
        view = this->menuBar()->addMenu("&View");
    
        QAction* toolbar = new QAction("&Toolbar", this);;
        toolbar->setCheckable(true);
        toolbar->setChecked(false);
        view->addAction(toolbar);
    
        QAction* points = new QAction("&Walkable points", this);;
        points->setCheckable(true);
        points->setChecked(false);
        view->addAction(points);
        
        QAction* creatures = new QAction("&Creatures Window", this);;
        creatures->setCheckable(true);
        creatures->setChecked(false);
        view->addAction(creatures);  
    
        QAction* dialog = new QAction("&Dialog Window", this);;
        dialog->setCheckable(true);
        dialog->setChecked(false);
        view->addAction(dialog);
    };
    

    public slots:
    void openFile()
    {
    QFileDialog::getOpenFileName( this, tr("Open Document"), QDir::currentPath(), tr("Document files (.doc .rtf);;All files (.)"), 0, QFileDialog::DontUseNativeDialog );

        QString filename = QFileDialog::getOpenFileName( 
            this, 
            tr("Open Document"), 
            QDir::currentPath(), 
            tr("Document files (*.doc *.rtf);;All files (*.*)") );
    
        if( !filename.isNull() )
        {
            qDebug( filename.toAscii() );
        }
    }
    
    void openFiles()
    {
        QStringList filenames = QFileDialog::getOpenFileNames( 
            this, 
            tr("Open Document"), 
            QDir::currentPath(), 
            tr("Documents (*.doc);;All files (*.*)") );
        if( !filenames.isEmpty() )
        {
            qDebug( filenames.join(",").toAscii() );
        }
    }
    
    void openDir()
    {
        QString dirname = QFileDialog::getExistingDirectory( 
            this, 
            tr("Select a Directory"), 
            QDir::currentPath() );
        if( !dirname.isNull() )
        {
            qDebug( dirname.toAscii() );
        }
    }
    
    void saveFile&#40;&#41;
    {
        QString filename = QFileDialog::getSaveFileName( 
            this, 
            tr("Save Document"), 
            QDir::currentPath(), 
            tr("Documents (*.doc)") );
        if( !filename.isNull() )
        {
            qDebug( filename.toAscii() );
        }
    }
    

    };

    int main(int argc, char *argv[ ])
    {
    QApplication app(argc, argv);

    MainWindow win; 
    win.showMaximized();
    return app.exec();
    

    }@

    This is my compile code:

    @
    #!/bin/bash
    qmake -project
    qmake
    make
    @

    I've already tried to delete the .o file and the .pro file. I think the problem may be that I'm doing the class definition and the class declaration in the same file? I wish that's not the problem because I don't want to split this little program in various files.. it's tedious.


  • Lifetime Qt Champion

    Hi and welcome to devnet,

    You need to declare your MainWindow in it's own header in order for moc to make it's job.


Log in to reply