unresolved external symbol "public: virtual struct QMetaObject const *
-
wrote on 28 Aug 2020, 08:54 last edited by Perdrix
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?
-
@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
@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
-
wrote on 28 Aug 2020, 08:58 last edited by
Try to clean the project and rebuild
-
wrote on 28 Aug 2020, 09:06 last edited by
When subclassing a QObject, you'd better write .h and .cpp separately.
Or there maybe some "mocing" problems... -
wrote on 28 Aug 2020, 09:10 last edited by
@gde23 said in unresolved external symbol "public: virtual struct QMetaObject const *:
Try to clean the project and rebuild
Tried that :(
-
When subclassing a QObject, you'd better write .h and .cpp separately.
Or there maybe some "mocing" problems...wrote on 28 Aug 2020, 09:11 last edited by@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 ...
-
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 this is inside main.cpp?
have you added #include "main.moc" at the end of the file?
-
@Perdrix this is inside main.cpp?
have you added #include "main.moc" at the end of the file?
wrote on 28 Aug 2020, 09:20 last edited by@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
-
wrote on 28 Aug 2020, 09:29 last edited by gde23
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. -
@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
@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
-
wrote on 28 Aug 2020, 10:20 last edited by
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
-
wrote on 22 Mar 2023, 08:02 last edited by
In the visual studio, we need to change the .h propeerty, set Item Type = "Qt Meta-Object Compiler (moc)".