compiler error C2248:'QObject::Qobject':cannot access private member declared in class 'QObject'

2.1k views Asked by At

Hi Everyone i have a foo class

Code in foo.h

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

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

  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

3

There are 3 answers

0
kersson On

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.

namespace GUI
{

class Connection : public QObject
{
    ...
};

}

Q_DECLARE_METATYPE(GUI::Connection)
2
peppe On

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.

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.

0
rca On

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.

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

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