【问题标题】:Issues while trying to define a class within a namespace (allocation of incomplete type)尝试在命名空间中定义类时出现问题(不完整类型的分配)
【发布时间】:2020-08-27 15:53:43
【问题描述】:

我正在尝试在 Ui 命名空间内的 Qt 中创建一个类。 在我的命名空间中,我已经有 2 个使用 Qt Designer 制作的类,使用它作为参考并不能帮助我解决问题。

myclass.h我放

namespace Ui {class MyClass};

class MyClass : public QObject
{Q_OBJECT
private:
    Ui::MyClass* ui;

//body
}

我把myclass.cpp放进去

MyClass::MyClass(QObject *parent) :QObject(parent),ui (new Ui::MyClass) 
//Here I have : allocation of incomplete type 'Ui::MyClass' 
{
    ui->setupUi(this);       

//Here I have : member access into incomplete type Ui::MyClass'
}

我看到了way of defining class in a namespace,但我没有找到解决我的错误的方法。

【问题讨论】:

  • namespace Ui {class MyClass} 不会编译。即使您添加缺少的; 以使该行有效,这仍然只是一个前向声明。您的类既没有在该命名空间中声明也没有定义。

标签: c++ namespaces


【解决方案1】:

这看起来不像是 Qt 问题。这只是 C++。要在命名空间中定义一个类,只需将声明放在命名空间中即可:

namespace Ui 
{
class MyClass : public QObject
{
    Q_OBJECT
private:
    MyClass* ui;

}; // MyClass
} // Ui

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-24
    • 1970-01-01
    • 2016-12-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多