W
W
WTFAYD2017-11-07 11:44:19
User interface
WTFAYD, 2017-11-07 11:44:19

Why does an undefined reference to vtable Progress error occur when trying to compile everything in one .cpp?

There is a sample code from Schlee's book about Qt 5.3. It is scattered on the Progress.h header, where the definition of the Progress class lies, Progress.cpp , where the definitions of the methods of this class lie, and the main file main.cpp . I threw everything into one main.cpp file.

// Как бы файл Progress.h
#include <QApplication>
#include <QtWidgets>
#include <QProgressBar>
#include <QPushButton>

// ======================================================================
class Progress : public QWidget {
    Q_OBJECT
private:
    QProgressBar* m_pprb;
    int           m_nStep;

public:
    Progress(QWidget* pobj = 0);

public slots:
    void slotStep ();
    void slotReset();
};

// ----------------------------------------------------------------------
Progress::Progress(QWidget* pwgt/*= 0*/)
    : QWidget(pwgt)
    , m_nStep(0)
{
    m_pprb = new QProgressBar;
    m_pprb->setRange(0, 5);
    m_pprb->setMinimumWidth(200);
    m_pprb->setAlignment(Qt::AlignCenter);

    QPushButton* pcmdStep  = new QPushButton("&Step");
    QPushButton* pcmdReset = new QPushButton("&Reset");

    QObject::connect(pcmdStep, SIGNAL(clicked()), SLOT(slotStep()));
    QObject::connect(pcmdReset, SIGNAL(clicked()), SLOT(slotReset()));

    //Layout setup
    QHBoxLayout* phbxLayout = new QHBoxLayout;
    phbxLayout->addWidget(m_pprb);
    phbxLayout->addWidget(pcmdStep);
    phbxLayout->addWidget(pcmdReset);
    setLayout(phbxLayout);
}
// Как бы файл Progress.cpp
// ----------------------------------------------------------------------
void Progress::slotStep()
{
    m_pprb->setValue(++m_nStep);
}

// ----------------------------------------------------------------------
void Progress::slotReset()
{
    m_nStep = 0;
    m_pprb->reset();
}
// Как бы файл main()
// ----------------------------------------------------------------------
int main (int argc, char** argv)
{
    QApplication app(argc, argv);
    Progress     progress;

    progress.show();

    return app.exec();
}

In this case, the file does not compile, errors fly out:
main.o: In function `Progress::Progress(QWidget*)':
/mnt/data-disk/MEGA/Programming/C++/build-untitled1-Desktop-Debug/../untitled1/main.cpp:24: undefined reference to `vtable for Progress'
/mnt/data-disk/MEGA/Programming/C++/build-untitled1-Desktop-Debug/../untitled1/main.cpp:24: undefined reference to `vtable for Progress'
main.o: In function `Progress::~Progress()':
/mnt/data-disk/MEGA/Programming/C++/build-untitled1-Desktop-Debug/../untitled1/main.cpp:7: undefined reference to `vtable for Progress'
/mnt/data-disk/MEGA/Programming/C++/build-untitled1-Desktop-Debug/../untitled1/main.cpp:7: undefined reference to `vtable for Progress'

Please tell me why is that?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Kormishin, 2017-11-07
@WTFAYD

The error means that not all virtual methods are defined, some "pure" virtual method is not defined in your class. Judging by the error, this is the Progress method, you need to add the virtual keyword , and it would be nice to add override ;)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question