1

这段代码:

QGraphicsObject *object=rootObject();
object->setProperty("width", 100);
QDeclarativeProperty(object, "width").write(100);

给出以下错误:

Invalid use of incomplete type 'struct QDeclarativeProperty'
4

1 回答 1

3

添加

#include <QDeclarativeProperty>

到源代码的顶部。似乎在您的代码中的某处,已经有一个前向声明QDeclarativeProperty,因此编译器知道它是一个类/结构,但缺少实际的类定义。

以下源代码通过编译器正确运行:

#include <QCoreApplication>
#include <QVariant>
#include <QDeclarativeProperty>

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

    QObject *object = 0; // rootObject();
    object->setProperty("width", 100);
    QDeclarativeProperty(object, "width").write(100);   
}

当你替换#include <QDeclarativeProperty>

class QDeclarativeProperty;

您会收到以下编译时错误:

main.cpp: In function 'int main(int, char**)':
main.cpp:11:38: error: invalid use of incomplete type 'struct QDeclarativeProperty'
main.cpp:4:7: error: forward declaration of 'struct QDeclarativeProperty'
于 2013-02-12T10:27:32.757 回答