Unresolved external symbol "public: virtual struct QMetaObject const * __thiscall Parent

I inherited a class from QObject:

class Parent: public QObject { Q_OBJECT QObject* cl; public: Parent(QObject *parent=0):QObject(parent) { cl = NULL; } QObject* getCl() const { return cl; } void setCl(QObject *obj) { cl = obj; } }; 

But when I write:

 Parent ev; 

I get the following error:

 main.obj:-1: error: LNK2001: unresolved external symbol "public: virtual struct QMetaObject const * __thiscall Parent::metaObject(void)const " (?metaObject@Parent@@UBEPBUQMetaObject@@XZ) main.obj:-1: error: LNK2001: unresolved external symbol "public: virtual void * __thiscall Parent::qt_metacast(char const *)" (?qt_metacast@Parent@@UAEPAXPBD@Z) main.obj:-1: error: LNK2001: unresolved external symbol "public: virtual int __thiscall Parent::qt_metacall(enum QMetaObject::Call,int,void * *)" (?qt_metacall@Parent@@UAEHW4Call@QMetaObject@@HPAPAX@Z) 
+48
c ++ qt qobject
Jan 05 '13 at 10:11
source share
17 answers

You must delete the debug folder of your application and run it again to fix this problem.

+58
Jan 05 '13 at 14:35
source share

If you are using Visual Studio, delete the Q_OBJECT line from the header file, save the file, put Q_OBJECT back in the header file, save the file again. This should generate the moc_* file, and it should build and link correctly.

+45
Apr 09 '13 at 22:10
source share

Unlike the name, the Rebuild Project will not destroy everything and will not work. If you recently added QObject to your class, you will have to run qmake again, for example.

  • Clear project
  • Run qmake
  • Assembly project

This is because, by default, qmake only starts when there are significant changes to your solution, such as adding new source files or modifying a .pro file. If you are editing an existing file, he does not know that he needs to run qmake . However, changing the QObject-ness class definitely requires qmake to run again.

If you are very paranoid, you can delete the Debug or Release folder. This will certainly force Qt to build everything from scratch.

+21
Jul 03 '13 at 6:22
source share

If your moc files are created in a visual studio project, try including them in the project if they are not included in the project and then rebuild.

+6
Dec 25 '14 at 12:07
source share

So the problem is that I need a Qt MOC compiler to compile my .h file. This is necessary for any classes that extend a QObject or one of its children. The fix (for me) is by right-clicking on the header file, selecting “Properties”, and setting the “Item Type” to “Enter MOC Mt”, then click “Compile” in the header, and then adding the resulting moc_myfilename.cpp file to mine project.

+5
Mar 27 '13 at 0:36
source share

I had the same problem in Visual Studio and solved it by following these steps:

  • Right-click the header file in Solution Explorer
  • The properties
  • Change "Item Type" to "Custom Build Tool"

Then in the Custom Build Tool configuration:

  • Go to General
  • set "Command Prompt" to:

    "$ (QTDIR) \ bin \ moc.exe" "% (FullPath)" -o ". \ GeneratedFiles \ $ (ConfigurationName) \ moc _% (file name) .cpp" "-fStdAfx.h" "-f. ./../../SRC/ FileName.h "-DUNICODE -DWIN32 -DWIN64 -DQT_DLL -DQT_NO_DEBUG -DNDEBUG -DQT_CORE_LIB -DQT_GUI_LIB -DQT_WIDGETS_LIB -DQT_NETWORK_LIB -DWIN32_LEAN_AND_MEAN -DDIS_VERSION = 7 -D_MATH_DEFINES_DEFINED" -I. \ SFML_STATIC "" -I. \ GeneratedFiles "" -I. " "-I $ (QTDIR) \ include" "-I. \ GeneratedFiles \ $ (ConfigurationName)." "-I $ (QTDIR) \ include \ QtCore" "-I $ (QTDIR) \ include \ QtGui" "-I $ (QTDIR) \ include \ QtNetwork"

  • set "Outputs" to:

    . \ GeneratedFiles \ $ (ConfigurationName) \ moc _% (file name) .cpp

  • set "Additional dependencies" to:
    $ (QTDIR) \ Bin \ moc.exe;% (FullPath)




Your exact values ​​may vary. They are usually applied through the Qt plugin.

+4
Aug 31 '16 at 2:39
source share

I use CMake to manage Qt projects, and the new Q_OBJECT needs to be added under the call QT4_WRAP_CPP. This will create moc _ *. Cxx to be included in the project and will clear unresolved external ones.

+3
Oct 28 '13 at 3:16
source share

In my case (using QtAdd-in with VS2012 and Qt v4.8.4) none of the above suggestions worked. For some reason, VS was unable to generate the proper moc files (assembly output: no matching classes were found. Not generated generated). When I compiled the corresponding headers manually (setting qt moc as a compiler and pressing "Compile"), it produced an empty moc file,

What was done to compile all the necessary mocs from the command line (moc -o moc_SomeClass.cpp SomeClass.h), and then replace the incorrect ones in the GeneratedFiles folder.

This is only a workaround (and not convenient for a large project) so that the project is successfully completed, but does not actually explain the strange behavior of VS / QtAdd-in.

+1
Jul 10 '13 at 9:31 on
source share

Using QtAdd-in with VS2010, I realized that the moc files are _ *. cpp were updated in the GeneratedFiles / Debug folder, although I was in release mode. Copying files to the Release folder worked for me.

+1
Jan 22 '14 at 8:22
source share

I had this problem with Visual Studio 2012 when I had a Q_OBJECT class definition in my cpp file. Moving the class definition to the header file resolved the issue.

It seems like it should be possible to maintain the Q_OBJECT class in the cpp file by adding the cpp file to the moc, but I have not tried this.

+1
May 18 '16 at 9:18
source share

I manually added cpp / ui files to my project, but forgot to add the header file explicitly as the header file. Now, when compiling, I received a similar error message, as indicated above, and the moc _ * files. Cpp were not generated in the assembly debug (or release) directory. This was not such an obvious error, qmake did not complain, and apart from the linker message, I had no errors.

So, if someone encounters the same problem again (or makes the same copy and pase error): make sure the header files are also added to the project file

+1
Jul 12 '16 at 9:09
source share

I had this problem with a "private class". Qt uses this model despite its code. I really liked it.

Basically, you have a privately declared class in the class definition of a public header file with a pointer to an instance of it as a member of the public class data. (Note. It may become easier for you to declare it as a friend class.)

Then create a private version of your class in the cpp file for the public. DO NOT create a header file for this private class. Do all the dirty work with this class. This hides the entire implementation of your public class, including other private members.

Without explaining what follows, here is an item related to this topic. To get Q_OBJECT working, I needed to add this to cpp:

 #include "MyPublicClass.moc" 

The cpp layout is as follows:

  • Private class defined
  • Mock for open class # included.
  • An implementation of an open class is defined.
0
Mar 06 '14 at 22:37
source share

This has happened to me recently when switching from MingW to MSVC. I had a class / structure prototype that was listed as a class, and MingW did not mind.

MSVC definitely sees the difference between class and struct when prototyping.

Hope one day someone helps someone else.

0
Oct 03 '14 at 20:27
source share

In my case, none of this was, but it was my mistake.

I redefined the virtual functions in the .h file (declared them), but I never defined them in .cpp :)

0
Feb 19 '15 at 16:35
source share

I solved the problem by adding it to the header file:

 #ifndef MYCLASSNAME_H #define MYCLASSNAME_H ... // all the header file content. #endif 
0
Jan 11 '16 at 2:36 on
source share

Any answer works for me in the VS 2013 environment. I ultimately solve the problem by removing the .h / .cpp file from the project and adding it back.

0
Aug 12 '16 at 19:02
source share

I work in VS2015 with the built-in Perforce p4v client. In my case, Perforce tried to add the moc file to the depot, when I returned this operation, Perforce removed this moc file from the project and deleted it. The file was recreated after the next compilation, but it was not included in the project, I had to add it manually to the "Generated Files" when I finally realized what the problem was.

0
Jul 09 '17 at 17:59
source share



All Articles