C++ “QObject::QObject”无法访问类“QObject”中声明的私有成员

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3507530/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 12:59:42  来源:igfitidea点击:

'QObject::QObject' cannot access private member declared in class 'QObject'

c++qtinheritanceqt4

提问by Sulla

class CHIProjectData : public QObject
{
public:
    CHIProjectData();
    CHIProjectData(QMap<QString,QString> aProjectData,
                   CHIAkmMetaData* apAkmMetaData = 0,
                   QObject* parent = 0);
private:
    QMap <QString,QString> m_strProjectData;
    CHIAkmMetaData* m_pAkmMetaData;
};

CHIProjectData::CHIProjectData(QMap<QString,QString> aProjectData,
                               CHIAkmMetaData* apAkmMetaData,
                               QObject* aParent)
    :
    QObject(aParent)
{
        m_strProjectData = aProjectData;
        m_pAkmMetaData = apAkmMetaData;
}

Why does it give the "'QObject::QObject' cannot access private member declared in class 'QObject'" error?

为什么会出现“'QObject::QObject'无法访问类'QObject'中声明的私有成员”错误?

采纳答案by Sulla

Adding a copy constructor to CHIProjectData class did the trick.

向 CHIProjectData 类添加一个复制构造函数就可以了。

回答by Caleb Huitt - cjhuitt

I'm guessing that your CHIProjectDataclass is being copied somewhere (using the compiler-generated copy constructor or assignment operator). QObjectcannot be copied or assigned to, so that would cause an error. However, the compiler has no line to point to for the error, so it chooses some line in the file (the final brace is common, since that is when the compiler knows if it should generate those functions or not, after parsing the class declaration to see if they already exist).

我猜你的CHIProjectData类被复制到某个地方(使用编译器生成的复制构造函数或赋值运算符)。 QObject无法复制或分配给,因此会导致错误。但是,编译器没有指向该错误的行,因此它选择文件中的某些行(最后一个大括号很常见,因为在解析类声明后,编译器知道它是否应该生成这些函数时)看看它们是否已经存在)。

回答by Igor Zevaka

The default constructor for QObjectmust be private and the error you are getting is quite likely to do with CHIProjectData::CHIProjectData(default constructor) implicitly trying to invoke base class's default constructor. If you look at QObjectyou would most likely find that it's defined something like this:

的默认构造函数QObject必须是私有的,并且您得到的错误很可能与CHIProjectData::CHIProjectData(默认构造函数)隐式地尝试调用基类的默认构造函数有关。如果你看一下,QObject你很可能会发现它的定义是这样的:

class QObject {
    QObject(); //private contructor, derived classes cannot call this constructor
public:
    QObject(QObject* aParent);
};

The solution is to make default QObjectconstructor protected or public or call other constructor overload from the default CHIProjectDataconstructor:

解决方案是将默认QObject构造函数设置为 protected 或 public 或从默认CHIProjectData构造函数调用其他构造函数重载:

CHIProjectData::CHIProjectData() : QObject(NULL){
}

回答by Isira

When using QObjectsubclass objects try to manipulate with pointers.

使用QObject子类对象时,请尝试使用指针进行操作。

take the problematic scenario

采取有问题的场景

myObject = MyObjectClass() 

in this case its more clean to have

在这种情况下,它更干净

MyObjectClass *myObject;
//code
myObject = new MyObjectClass;

This would remove the need for object copying and assignments by using reference copying and assignments.

这将通过使用引用复制和分配消除对象复制和分配的需要。

回答by virt

In my case the problem was that the Q_OBJECTmacro silently introduces a private:specifier, even within a struct:

在我的例子中,问题是 Q_OBJECT宏默默地引入了一个private:说明符,即使在一个结构中:

struct myClass : public QObject {
   Q_OBJECT
   // everything here is private now...
}