4

I have Qt libraries 4.8.1 for Windows (VS 2010 ultimate) with Qt Visual Studio Add-in. This is my very simple app :

#include<qobject.h>
#include<qstring.h>
#include<memory>

class MyClass : public QObject{
Q_OBJECT

public:
MyClass( const QString &text, QObject *parent = 0 ) : m_text(text) {}

public slots:
void setText( const QString &text );

signals:
void textChanged( const QString& );

private:
QString m_text;
};

void MyClass::setText( const QString &text ){
if( m_text == text ) return;
m_text = text;
emit textChanged( m_text );
}

int main(int argc, char *argv[]){
    std::shared_ptr<MyClass> a(new MyClass("foo"));
    std::shared_ptr<MyClass> b(new MyClass("bar"));

QObject::connect( a.get(), SIGNAL(textChanged(const QString&)),
b.get(), SLOT(setText(const QString&)) );
a->setText("changed");
}

Errors I get related to unresolved externals : Error 6 error LNK2001: unresolved external symbol "public: virtual int __thiscall MyClass::qt_metacall(enum QMetaObject::Call,int,void * *)" (?qt_metacall@MyClass@@UAEHW4Call@QMetaObject@@HPAPAX@Z)

Error 4 error LNK2001: unresolved external symbol "public: virtual struct QMetaObject const * __thiscall MyClass::metaObject(void)const " (?metaObject@MyClass@@UBEPBUQMetaObject@@XZ)

Error 5 error LNK2001: unresolved external symbol "public: virtual void * __thiscall MyClass::qt_metacast(char const *)" (?qt_metacast@MyClass@@UAEPAXPBD@Z)

Error 3 error LNK2019: unresolved external symbol "protected: void __thiscall MyClass::textChanged(class QString const &)" (?textChanged@MyClass@@IAEXABVQString@@@Z) referenced in function "public: void __thiscall MyClass::setText(class QString const &)" (?setText@MyClass@@QAEXABVQString@@@Z)

and two warnings :

Warning 1 warning MSB8017: A circular dependency has been detected while executing custom build commands for item "GeneratedFiles\Debug\main.moc". This may cause incremental build to work incorrectly.

Warning 2 warning : No resources in 'C:\Users\Anonymous\documents\visual studio 2010\Projects\qtWorld\qtWorld\qtworld.qrc'.

I didn't use any qmake / nmake . They ain't required when you get latest Qt Visual Studio Add-in 1.1.11 (even Intellisense recognizes the keywords slots: signals:), right?

Now my questions :

  1. I heard many errors are resolved by just rebuilding the whole solution , why is that ?
  2. Please explain why I get these errors in deep and their possible solutions.

p.s please be good at explanation, don't be rep whorer

2 Answers 2

4

If you have QObject class defined in cpp file then you should also include moc for that file. For example your MyClass is defined in main.cpp then you should add

#include "main.moc"

to end of your main.cpp file.

Thanks to @Tomma for providing the unexplained answer :P

Explanation why above code didn't work :

However, the moc is typically applied to header files only. Since your class definition is part of the main program file, the IDE does not recognize it.

Thanks to @koahnig for providing nice explanation

1

The constructor of your class is not defined, only declared. Same thing for the textChanged() method. At least that is what I see from the code that you posted. Define them and the error should go away.

4
  • moc is supposed to create the implementation for the signal textChanged()
    – drescherjm
    Aug 14, 2012 at 10:56
  • textChanged is signal , how can define it? . though I defined the constructor now it's still giving same warning and errors . Read my questions again please
    – Mr.Anubis
    Aug 14, 2012 at 11:11
  • Although I use the addon in Visual Studio I do not use the automated moc function (I am using CMake instead) so I am unsure how that works. If moc is being run you should have a moc_ file in your build folder somewhere that includes the implementation. I can not help at all with getting the addon to work.
    – drescherjm
    Aug 14, 2012 at 11:31
  • I am also unable to help with the moc part, sorry. Aug 14, 2012 at 12:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.