QFileSystemWatcher未解析外部

时间:2017-06-06 12:32:42

标签: c++ qt

我跟随this example摆弄QFileSystemWatcher。但是在编译时我得到了一个未解决的外部符号错误。 这是我的代码:

#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();
}

编译时产生以下错误消息:

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

我做错了什么?

1 个答案:

答案 0 :(得分:1)

尝试将MyClass定义放在单独的.h文件中