Question

I'm developing C++ Qt application. I have one abstract class and two children. One of the children works exactly the way it should, but the second one causes an error:

Freewer.obj:-1: Error: LNK2001: unresolved external symbol "public: virtual void __thiscall Sierpinski::render(class QPainter &,class QRect)" (?render@Sierpinski@@UAEXAAVQPainter@@VQRect@@@Z)

File not found: Freewer.obj

The problem is, that both of the children are defined the same way.
Let's show you my code.

Fractal.h:

#ifndef FRACTAL_H
#define FRACTAL_H

#include <QPainter>
#include <QRect>
#include <QDebug>

class Fractal
{
public:
    virtual void render(QPainter &painter, QRect target) = 0;
};

#endif // FRACTAL_H

Cantor.h:

#ifndef CANTOR_H
#define CANTOR_H

#include "Fractal.h"

class Cantor : public Fractal
{
public:
    void render(QPainter &painter, QRect target) Q_DECL_OVERRIDE;
};

#endif // CANTOR_H

Cantor.cpp:

#include "Cantor.h"

void Cantor::render(QPainter &painter, QRect target)
{
    Q_UNUSED(painter);
    Q_UNUSED(target);

    qDebug() << "Rendering Cantor's Discontinuum...";
}

Sierpinski.h:

#ifndef SIERPINSKI_H
#define SIERPINSKI_H

#include "Fractal.h"

class Sierpinski : public Fractal
{
public:
    void render(QPainter &painter, QRect target) Q_DECL_OVERRIDE;
};

#endif // SIERPINSKI_H

Sierpinski.cpp:

#include "Sierpinski.h"

void Sierpinski::render(QPainter &painter, QRect target)
{
    Q_UNUSED(painter);
    Q_UNUSED(target);

    qDebug() << "Rendering Sierpinski triangle...";
}

Now, when I want to create an instance of Cantor class, it works properly. But when I want to create an instance of Sierpinski class, it causes the error I wrote above.

When I change Sierpinsky header file this way:

Sierpinski.h:

#ifndef SIERPINSKI_H
#define SIERPINSKI_H

#include "Fractal.h"

class Sierpinski : public Fractal
{
public:
    void render(QPainter &painter, QRect target)
    {
        Q_UNUSED(painter);
        Q_UNUSED(target);

        qDebug() << "Rendering Sierpinski triangle...";
    }
};

#endif // SIERPINSKI_H

and I won't use Sierpinski.cpp, everything works properly. Isn't it weird? Can you help me please? Where do I have any mistake? I'd like to use Sierpinski definition the same way as Cantor's.

Thank you very much.

Edit:

After the question of @Joachim Isaksson I tried to add another one fractal the same way as Cantor and Sierpinski and I've got the same error as from Sierpinski. The new class is called Koch and it's not working. But when I leave Koch.cpp file out and I use definition of render function directly in header file, everything is ok.

Was it helpful?

Solution

First of all, you are misusing the term "children". What you mean by that is subclasses. Children means something different in the Qt world.

The probable reason is that you do not have the corresponding source files in your SOURCES variable in your qmake project file.

Once you make that modiication, make sure qmake is rerun properly.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top