Solved unresolved external symbol "public: virtual struct QMetaObject const *
-
I added this:
class DSSImageToolBar : public QToolBar { Q_OBJECT public: explicit DSSImageToolBar(QWidget* parent) : QToolBar(parent), opacityEffect(this) { } ~DSSImageToolBar() {}; void setOpacity(qreal opacity) { opacityEffect.setOpacity(opacity); setGraphicsEffect(&opacityEffect); update(); }; protected: void enterEvent(QEvent* e) override { setOpacity(1.0); }; void leaveEvent(QEvent* e) override { setOpacity(0.5); }; QGraphicsOpacityEffect opacityEffect; };
to the front of my main.cpp file after the includes.
For my pains I got:
1>main.obj : error LNK2001: unresolved external symbol "public: virtual struct QMetaObject const * __cdecl DSSImageToolBar::metaObject(void)const " (?metaObject@DSSImageToolBar@@UEBAPEBUQMetaObject@@XZ) 1>main.obj : error LNK2001: unresolved external symbol "public: virtual void * __cdecl DSSImageToolBar::qt_metacast(char const *)" (?qt_metacast@DSSImageToolBar@@UEAAPEAXPEBD@Z) 1>main.obj : error LNK2001: unresolved external symbol "public: virtual int __cdecl DSSImageToolBar::qt_metacall(enum QMetaObject::Call,int,void * *)" (?qt_metacall@DSSImageToolBar@@UEAAHW4Call@QMetaObject@@HPEAPEAX@Z)
If I put into a separate header it all works fine, but I didn't want to externalise that class definition.
So why does it fail in the cpp but work in a header?
-
@Perdrix said in unresolved external symbol "public: virtual struct QMetaObject const *:
At the end of main.cpp?
yes,
The problem is, that your class definition is located inside a source file, (main.cpp in this case) and that is messing up qmake
take a look at this stack overflow answer, a very good one
https://stackoverflow.com/a/34929627I'm not sure if its explicitly featured in the Meta-Object-Compiler documentation 🤷♂️
edit:
just checked, it is mentioned
https://doc.qt.io/qt-5/moc.html#writing-make-rules-for-invoking-moc
-
Try to clean the project and rebuild
-
When subclassing a QObject, you'd better write .h and .cpp separately.
Or there maybe some "mocing" problems... -
@gde23 said in unresolved external symbol "public: virtual struct QMetaObject const *:
Try to clean the project and rebuild
Tried that :(
-
@Bonnie said in unresolved external symbol "public: virtual struct QMetaObject const *:
When subclassing a QObject, you'd better write .h and .cpp separately.
Or there maybe some "mocing" problems...So I see - not ideal ...
-
@Perdrix this is inside main.cpp?
have you added #include "main.moc" at the end of the file?
-
@J-Hilk said in unresolved external symbol "public: virtual struct QMetaObject const *:
@Perdrix this is inside main.cpp?
have you added #include "main.moc" at the end of the file?
At the end of main.cpp? Looking at the generated main.moc file, it seems probable that this would help. What part of the docs would tell me that I needed to do that? I failed to spot anything in the "Using the Meta-Object Compiler" part
-
EDIT: I just tested it and it is working for me with class being in .h file.
If the class is somewhere in a source file it does not work. -
@Perdrix said in unresolved external symbol "public: virtual struct QMetaObject const *:
At the end of main.cpp?
yes,
The problem is, that your class definition is located inside a source file, (main.cpp in this case) and that is messing up qmake
take a look at this stack overflow answer, a very good one
https://stackoverflow.com/a/34929627I'm not sure if its explicitly featured in the Meta-Object-Compiler documentation 🤷♂️
edit:
just checked, it is mentioned
https://doc.qt.io/qt-5/moc.html#writing-make-rules-for-invoking-moc
-
It's sort of documented there but I never wrote a make rule for this as I am using Visual Studio ...
Which means I'd never have looked there! Should be clear and up front in the moc documentation ...
Thanks for the explanation
-
In the visual studio, we need to change the .h propeerty, set Item Type = "Qt Meta-Object Compiler (moc)".