编译错误C2248:“QObject: QObject”:无法访问类“QObject”中声明的私有成员

时间:2021-01-24 18:43:51

Hi Everyone i have a foo class

大家好,我有一个foo类。

Code in foo.h

代码foo。

namespace GUI
{
class Foo : public QObject
{
Q_OBJECT

public:
explicit Foo(QObject *parent = 0);
virtual ~Foo();
....


};
}

Now this is working and compiling fine but i want to save this custom c++ class using QSettings and one of the step is registering your class with Q_DECLARE_METATYPE

现在这是工作和编译好的,但是我想用QSettings保存这个自定义c++类,其中一个步骤是用Q_DECLARE_METATYPE注册您的类。

and therefore as soon as i put this line Q_DECLARE_METATYPE(Foo) at the end of my foo.h file i get these compiler error C2248:'QObject::Qobject':cannot access private member declared in class 'QObject' and which when clicked only takes me to last line of my connection.h header file and gives no other information as to what might be wrong i,e the error point me to

因此,当我在Foo的末尾添加了这行Q_DECLARE_METATYPE(Foo)时。h文件我得到了这些编译器错误C2248:“QObject: QObject”:无法访问类“QObject”中声明的私有成员,在单击时只会将我连接到最后一行。h头文件,并没有提供其他信息,关于什么可能是错误的,e错误指向我。

  Class Foo
  {

  };<---- point me here
  Q_DECLARE_METATYPE(Foo)

I know that QObject cannot be copied and this is related to it but i have no idea what maybe wrong here and how can i rectify it. Thanks in advance

我知道QObject不能被复制,这和它有关,但我不知道这里可能有什么问题,我该如何改正它。谢谢提前

3 个解决方案

#1


2  

I know that QObject cannot be copied and this is related to it but i have no idea what maybe wrong here and how can i rectify it.

我知道QObject不能被复制,这和它有关,但我不知道这里可能有什么问题,我该如何改正它。

It is related. Q_DECLARE_METATYPE requires your type to be copiable, but your type inherits from QObject, so you can't do that. Sure, you could instead Q_DECLARE_METATYPE(Foo*), but I think you should instead move the settings into a separate value class, which then you can save using QSettings.

这是相关的。Q_DECLARE_METATYPE要求您的类型是可复制的,但是您的类型继承了QObject,所以您不能这样做。当然,您可以使用Q_DECLARE_METATYPE(Foo*),但是我认为您应该将设置移动到一个单独的值类中,这样您就可以使用QSettings保存。

#2


0  

Make sure your Q_DECLARE_METATYPE statement is outside of your namespace and that you fully qualify your class name. See the Q_DECLARE_METATYPE doc for more detail.

确保您的Q_DECLARE_METATYPE语句在您的名称空间之外,并且您完全限定了类名。请参阅Q_DECLARE_METATYPE doc以获得更详细的信息。

namespace GUI
{

class Connection : public QObject
{
    ...
};

}

Q_DECLARE_METATYPE(GUI::Connection)

#3


0  

I know that QObject cannot be copied and this is related to it but i have no idea what maybe wrong here and how can i rectify it.

我知道QObject不能被复制,这和它有关,但我不知道这里可能有什么问题,我该如何改正它。

So declare a copy constructor, then it will be ok.

因此,声明一个复制构造函数,就可以了。

...
Foo(const Foo &_other);
...

#1


2  

I know that QObject cannot be copied and this is related to it but i have no idea what maybe wrong here and how can i rectify it.

我知道QObject不能被复制,这和它有关,但我不知道这里可能有什么问题,我该如何改正它。

It is related. Q_DECLARE_METATYPE requires your type to be copiable, but your type inherits from QObject, so you can't do that. Sure, you could instead Q_DECLARE_METATYPE(Foo*), but I think you should instead move the settings into a separate value class, which then you can save using QSettings.

这是相关的。Q_DECLARE_METATYPE要求您的类型是可复制的,但是您的类型继承了QObject,所以您不能这样做。当然,您可以使用Q_DECLARE_METATYPE(Foo*),但是我认为您应该将设置移动到一个单独的值类中,这样您就可以使用QSettings保存。

#2


0  

Make sure your Q_DECLARE_METATYPE statement is outside of your namespace and that you fully qualify your class name. See the Q_DECLARE_METATYPE doc for more detail.

确保您的Q_DECLARE_METATYPE语句在您的名称空间之外,并且您完全限定了类名。请参阅Q_DECLARE_METATYPE doc以获得更详细的信息。

namespace GUI
{

class Connection : public QObject
{
    ...
};

}

Q_DECLARE_METATYPE(GUI::Connection)

#3


0  

I know that QObject cannot be copied and this is related to it but i have no idea what maybe wrong here and how can i rectify it.

我知道QObject不能被复制,这和它有关,但我不知道这里可能有什么问题,我该如何改正它。

So declare a copy constructor, then it will be ok.

因此,声明一个复制构造函数,就可以了。

...
Foo(const Foo &_other);
...