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

后端 未结 23 2192
醉话见心
醉话见心 2020-11-30 21:33

I inherited a class from QObject :

class Parent: public QObject
{
    Q_OBJECT
    QObject* cl;

public:
    Parent(QObject *parent=0):QObject(parent) {
            


        
相关标签:
23条回答
  • 2020-11-30 22:16

    I added cpp/ui files to my project manually, but forgot to add the header file explicitly as header file. Now when compiling I got a similar error message as above and the moc_*.cpp file(s) were not generated in the debug (or release) directory of the build. That was not such an obvious mistake, qmake did not complain and other than the linker message I got no errors.

    So if anyone encounters the same problem again (or makes the same copy & pase mistake): make sure the header files have also been added to your project file

    0 讨论(0)
  • 2020-11-30 22:17

    I noticed some answers are based on Visual Studio.

    This answer is based on Qt Creator.

    Unlike the name suggest, Rebuild Project will not wipe out everything and build from scratch. If you recently added QObject (and/or Q_OBJECT) to your class, you'll have to run qmake again, e.g.

    1. Clean Project
    2. Run qmake
    3. Build Project

    This is because, by default, qmake only runs when you do significant changes to your solution like adding new source files or modify the .pro file. If you make edits to an existing file, it doesn't know it needs to run qmake.

    As a fall back, to brute force Qt to build everything from scratch, delete the Debug or Release folder.

    0 讨论(0)
  • 2020-11-30 22:17

    If your moc files are generated in the visual studio project try to include them into project if they are not included into project then rebuild.

    0 讨论(0)
  • 2020-11-30 22:20

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

    1. Right-click header file in solution Explorer
    2. Properties
    3. Change "Item Type" to "Custom Build Tool"

    Then in the Custom Build Tool configuration:

    1. Go to General
    2. set "Command Line" to:

      "$(QTDIR)\bin\moc.exe" "%(FullPath)" -o ".\GeneratedFiles\$(ConfigurationName)\moc_%(Filename).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"

    3. set "Outputs" to:

      .\GeneratedFiles\$(ConfigurationName)\moc_%(Filename).cpp

    4. set "Additional Dependencies" to:
      $(QTDIR)\bin\moc.exe;%(FullPath)


    Your exact values may be different. They are usually applied via a Qt plugin.

    0 讨论(0)
  • 2020-11-30 22:21

    Using QtAdd-in with VS2010 i realized the moc_*.cpp files were updated in the GeneratedFiles/Debug folder although i was in release mode. Copying the files into the Release folder worked for me.

    0 讨论(0)
  • 2020-11-30 22:22

    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 looks like it should be possible to support Q_OBJECT class in cpp file by adding the cpp file to moc but I did not try this.

    0 讨论(0)
提交回复
热议问题