0

I'm fiddling around with the QFileSystemWatcher, following this example. But when compiling I'm getting an unresolved external symbol error. Here is my code:

#include <QCoreApplication>
#include <QFileSystemWatcher>
#include <QObject>

#include <iostream>

class MyClass : public QObject
{
    Q_OBJECT

public:
    MyClass(QObject* parent = nullptr) : QObject(parent){}

public slots:
    void on_dir_change(const QString& path)
    {
        std::cout << "folder modified: (" << path.toStdString() << ")" << 
std::endl;
    }
};

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    QFileSystemWatcher watcher;
    watcher.addPath("C:/test");

    MyClass* mc = new MyClass();

    QObject::connect(&watcher, SIGNAL(directoryChanged(QString)), mc, 
SLOT(on_dir_change(QString)));

    return a.exec();
}

Which on compiling yields the following error messages:

main.obj : error LNK2001: unresolved external symbol "public: virtual struct QMetaObject const * __cdecl MyClass::metaObject(void)const " (?metaObject@MyClass@@UEBAPEBUQMetaObject@@XZ)

main.obj : error LNK2001: unresolved external symbol "public: virtual void * __cdecl MyClass::qt_metacast(char const *)" (?qt_metacast@MyClass@@UEAAPEAXPEBD@Z)

main.obj : error LNK2001: unresolved external symbol "public: virtual int __cdecl MyClass::qt_metacall(enum QMetaObject::Call,int,void * *)" (?qt_metacall@MyClass@@UEAAHW4Call@QMetaObject@@HPEAPEAX@Z)

debug\FileSystemWatcherTest.exe : fatal error LNK1120: 3 unresolved externals

What am I doing wrong?

DenverCoder21
  • 879
  • 3
  • 16
  • 34
  • Can you give the error message ? Which function can not be resolved ? – Hải Phạm Lê Jun 06 '17 at 12:37
  • 2
    You haven't implemented `MyClass::MyClass(class QObject *)`. – G.M. Jun 06 '17 at 12:40
  • I implemented the constructor (see edit), but still get errors. Did I implement wrong? – DenverCoder21 Jun 06 '17 at 12:46
  • The errors you're seeing now are because you haven't linked with the code generated by `moc`. I'm not sure what build tools you're using but take a look at the [documentation](http://doc.qt.io/qt-5/gettingstartedqt.html). – G.M. Jun 06 '17 at 13:16
  • I'm using QtCreator on Windows (Visual Studio 2015), Kit is the autodetected one. Project file is a .pro QtCreator project using QMake. I started out with by selecting a new Qt Console Application in the Wizard - is that maybe an issue? – DenverCoder21 Jun 06 '17 at 13:24
  • @DenverCoder21, is there a .moc file in your project (should be)? You have to compile it too. – vahancho Jun 06 '17 at 13:32
  • @vahanco, no there is not. What could be the reason for this? – DenverCoder21 Jun 06 '17 at 13:35

1 Answers1

1

Try to place MyClass definition in a separate .h file

olya
  • 312
  • 1
  • 4
  • How would that help? As I understand, the #include of the .h file would simply copy the content of the .h file into the main.cpp. – DenverCoder21 Jun 06 '17 at 13:23
  • It would help because moc needs it. Else you need to include it manually at the end of your code, something like: _#include "filename.moc"_ – Zlatomir Jun 06 '17 at 13:48
  • This really works, thank you! Could you elaborate why this is needed for moc or provide a link so I can get a better understanding? – DenverCoder21 Jun 06 '17 at 13:53
  • @DenverCoder21, Qt docs say: "The moc tool reads a C++ **header file**. If it finds one or more class declarations that contain the Q_OBJECT macro, it produces a C++ source file containing the meta-object code for those classes". http://doc.qt.io/qt-5/moc.html – vahancho Jun 07 '17 at 09:06