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

What am I failing to #include?



  • I've been trying to derive a class from QGraphicsItem, but I keep getting a linker error with an unresolved external symbol. I've stripped my classes down to the minimum requirements for a QGraphicsItem, I believe, but since it's still throwing the error if I try to instantiate that class I'm thinking that I've not included a part of Qt that I need to. I've included everything that the Chip example includes in its equivalent classes, so I'm not sure what I'm missing.

    Here's what I have after stripping everything:

    block.h

    #ifndef BLOCK_H
    #define BLOCK_H
    
    #include <QColor>
    #include <QGraphicsItem>
    
    class Block : public QGraphicsItem
    {
    public:
        Block();
        QRectF boundingRect() const override;
        QPainterPath shape() const override;
        void paint(QPainter *painter, const QStyleOptionGraphicsItem *item, QWidget *widget) override;
    };
    
    #endif // BLOCK_H
    

    block.cpp

    #include "block.h"
    
    #include <QtWidgets>
    #include <QColor>
    #include <QGraphicsItem>
    
    Block::Block()
    {
    }
    
    QRectF Block::boundingRect() const
    {
        return QRectF(0, 0, 110, 70);
    }
    
    QPainterPath Block::shape() const
    {
        QPainterPath path;
        path.addRect(14, 14, 82, 42);
        return path;
    }
    
    void Block::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
    {
        Q_UNUSED(widget)
        Q_UNUSED(option)
    
        painter->drawRoundedRect(-10, -10, 20, 20,5, 5);
    
    }
    

    I've been testing whether it will link or not by just adding this to the MainWindow:

    Block *block = new Block;
    

  • Lifetime Qt Champion

    @idkwtph said in What am I failing to #include?:

    but I keep getting a linker error with an unresolved external symbol.

    Could you tell us, which error you exactly get? It's hard to help otherwise.

    Regards



  • @idkwtph

    I've been trying to derive a class from QGraphicsItem, but I keep getting a linker error with an unresolved external symbol.

    Most importantly like @aha_1980 said you need to supply the error message!

    But just so you know: when you get an "unresolved" from the linker, it's (usually) not that you are failing to #include something. Rather it's that you're failing to supply a library to the linker. When you #include a third-party's header files the documentation will usually show you what library you will need, and sometimes those need adding (to the project/makefile).



  • The error I'm getting is

    azmainwindow.obj:-1: error: LNK2019: unresolved external symbol "public: __cdecl Block::Block(void)" (??0Block@@QEAA@XZ) referenced in function "public: __cdecl AzMainWindow::AzMainWindow(class QWidget *)" (??0AzMainWindow@@QEAA@PEAVQWidget@@@Z)
    

    Googling suggests that that error is caused by not including a library or by not defining something. I'm pretty sure the only library I would need is Qt since the example I'm using was included with Qt itself, and I picked through everything I listed to make sure that I supplied a definition for every function in the Block class.


  • Lifetime Qt Champion

    Hi,

    Any chances that you have that class in a library ?

    If so, take look at the Creating Shared Libraries chapter of Qt's documentation.

    You are missing the proper import/export expressions, that chapter shows how to make them properly.


  • Lifetime Qt Champion

    @idkwtph
    Hi
    it complains about the constructor but you have one that looks like what its expecting. ( Block(); )
    So you should try to delete the build folder to make sure it
    completely compiles all again.



  • I don't know exactly what happened, but I think I found the solution. The project wasn't creating the .obj file properly for the main window, and while I'm still not totally sure why, it turns out that 'run qmake' fixed the issue. It now links properly when I build the project.


Log in to reply