0

I have a project with subprojects in Qt/C++ where I try to run the project with unit tests:

enter image description here

But when I try to build a project, I get the following error:

Undefined symbols for architecture x86_64:
  "vtable for MainWindow", referenced from:
      MainWindow::MainWindow(QWidget*) in tests.o
  NOTE: a missing vtable usually means the first non-inline virtual member function has no definition.
  "vtable for PaintScene", referenced from:
      PaintScene::PaintScene(QObject*) in tests.o
  NOTE: a missing vtable usually means the first non-inline virtual member function has no definition.
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[1]: *** [tests] Error 1

But all constructors are declared and defined.

paintscene:

PaintScene(QObject *parent = nullptr);

PaintScene::PaintScene(QObject *parent) : QGraphicsScene(parent)
{
    /***/
}

mainwindow:

MainWindow(QWidget *parent = nullptr);

MainWindow::MainWindow(QWidget *parent) : QWidget(parent)
{
    /***/
}

tests.cpp:

#include <QtTest>
#include <QCoreApplication>
#include </Users/garbart/Desktop/paint/paint/mainwindow.cpp>
#include </Users/garbart/Desktop/paint/paint/paintscene.cpp>

class UnitTests : public QObject
{
    Q_OBJECT
private slots:
    void testWidth();
    void testColor();
    void testScene();
};

void UnitTests::testWidth()
{
    PaintScene *scene = new PaintScene();
    scene->setWidth(1000);
    QCOMPARE(scene->_width, 10);
    scene->setWidth(100);
    QCOMPARE(scene->_width, 100);
    scene->setWidth(-10);
    QCOMPARE(scene->_width, 100);
}

void UnitTests::testColor()
{
    QColor qc = Qt::red;
    PaintScene *scene = new PaintScene();
    scene->set_color(qc);
    QCOMPARE(scene->_color, Qt::red);

    qc = Qt::green;
    scene->set_color(qc);
    QCOMPARE(scene->_color, Qt::green);

    qc = Qt::black;
    scene->set_color(qc);
    QCOMPARE(scene->_color, Qt::black);
}

void UnitTests::testScene()
{
    MainWindow *window = new MainWindow();

    QColor test_color = window->grab(QRect(window->rect().x() + 500, window->rect().y() + 500, 1, 1)).toImage().pixelColor(0,0);
    QCOMPARE(test_color, Qt::white);
}

QTEST_MAIN(UnitTests)

#include "tests.moc"
Alexander V
  • 8,351
  • 4
  • 38
  • 47
garbart
  • 465
  • 4
  • 19
  • 1
    The problem is likely with symbols not being picked up by the unit-test project. Unit-test is a separate executable, right? Then it has own main.cpp and its project should refer to other files by itself? I also doubt that unit-test needs to include main window from the app. – Alexander V May 14 '18 at 17:17

1 Answers1

0

It looks like a classical linker error (we can't be 100% sure if you do not show us your mainwindow.h header file) , you should export your MainWindow class if you want to use it in your test project (outside of your paint project).

see How to export a C++ class from a dll?

sandwood
  • 2,038
  • 20
  • 38