Qt 向QFile添加方法-编译错误“;未定义的引用…“;

Qt 向QFile添加方法-编译错误“;未定义的引用…“;,qt,Qt,我想向QFile添加一个名为chkFileExists()的方法。我有以下用于扩展QFile的代码文件: mvqfile.h #ifndef MVQFILE_H #define MVQFILE_H #include <QFile> class MVQFile : public QFile { Q_OBJECT public: explicit MVQFile(QObject *parent = 0); bool chkFileExists(const QS

我想向QFile添加一个名为chkFileExists()的方法。我有以下用于扩展QFile的代码文件:

mvqfile.h

#ifndef MVQFILE_H
#define MVQFILE_H

#include <QFile>

class MVQFile : public QFile
{
    Q_OBJECT
public:
    explicit MVQFile(QObject *parent = 0);
    bool chkFileExists(const QString &file);
};

#endif // MVQFILE_H

为什么??在我看来,这似乎是正确的。

您忘了将名称空间添加到函数定义中。应该是:

bool MVQFile::chkFileExists(const QString &file) {

}

啊!感谢您的快速回答。作为旁注,为什么
chkFileExists
是一个实例方法,但它以文件名作为参数?另外,省略
ec
以在方法名的15个字符中保存两个字符也很奇怪。这是一个好的观点。第一个发生在我刚刚将这个函数移到类中并忘记的时候。现在修好了。第二个是个人偏好。
#include "mvqfile.h"


MVQFile file;
file.setFileName("/home/path/filename.csv");

if (file.chkFileExists(file.fileName()))
{
  qDebug() << file.fileName() << " exists";
} else {
  qDebug() << file.fileName() << " does not exist";
}
"undefined reference to `MVQFile :: chkFileExists(QString const&) ' "
bool MVQFile::chkFileExists(const QString &file) {

}