【发布时间】:2020-10-16 03:39:40
【问题描述】:
代码是什么
我正在尝试通过学习 Qt 5 这本书来学习 Qt c++。这是后续代码。给出错误的特定部分用于编写预设的欢迎消息。 Qml 代码使用头文件中的 Q_PROPERTY 宏来访问该函数。 这是实际标头中的函数定义和有用的宏:
Q_PROPERTY( QString ui_welcomeMessage MEMBER welcomeMessage CONSTANT )
const QString& welcomeMessage() const;
错误无效使用非静态成员
错误分为两部分,但它们似乎相互关联。第一个似乎表明我没有正确使用 const QString& 作为我的成员函数的输出:
/home/sina/Documents/code/Qt/Learn Qt 5/client_management/client_management-lib/build/linux
/gcc/x64/debug/.moc/moc_master-controller.cpp:83:
error: invalid use of non-static member function ‘const QString&
cm::controllers::MasterController::welcomeMessage() const’
../../client_management/client_management-lib/build/linux/gcc/x64/debug/.moc/moc_master-
controller.cpp:83:56: error: invalid use of non-static member function ‘const QString&
cm::controllers::MasterController::welcomeMessage() const’
83 | case 0: *reinterpret_cast< QString*>(_v) = _t->welcomeMessage; break;
| ^~~~~~~~~~~~~~
对于 moc 文件中的错误,大多数建议只在开始时使用 moc 清理和删除所有文件。在那之后不起作用,我尝试删除自动生成的所有内容。
moc 文件中的错误 #error
错误的第二部分是指同一个文件。这是添加到 moc 文件顶部的几行代码:
#include <memory>
#include "../../../../../../controllers/master-controller.h"
#include <QtCore/qbytearray.h>
#include <QtCore/qmetatype.h>
#if !defined(Q_MOC_OUTPUT_REVISION)
#error "The header file 'master-controller.h' doesn't include <QObject>."
#elif Q_MOC_OUTPUT_REVISION != 67
#error "This file was generated using the moc from 5.15.1. It"
#error "cannot be used with the include files from this version of Qt."
#error "(The moc has changed too much.)"
#endif
【问题讨论】:
-
在第 83 行,如果你有
_t->welcomeMessage();而不是_t->welcomeMessage;怎么办 -
@NgocMinhNguyen 我检查标记的答案实际上最终起作用了,所以我没有尝试你的答案。唯一的问题是 moc 文件是自动生成的,因此更改它可能是最好的长期解决方案。