Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 254
Sulla Avatar asked Aug 17 '10 22:08

Sulla


5 Answers

I'm guessing that your CHIProjectData class is being copied somewhere (using the compiler-generated copy constructor or assignment operator). QObject cannot 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).

like image 109
Caleb Huitt - cjhuitt Avatar answered Dec 18 '22 12:12

Caleb Huitt - cjhuitt


The default constructor for QObject must 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 QObject you would most likely find that it's defined something like this:

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

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

CHIProjectData::CHIProjectData() : QObject(NULL){
}
like image 27
Igor Zevaka Avatar answered Dec 18 '22 14:12

Igor Zevaka


Adding a copy constructor to CHIProjectData class did the trick.

like image 20
Sulla Avatar answered Dec 18 '22 12:12

Sulla


When using QObject subclass objects try to manipulate with pointers.

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.

like image 39
Isira Avatar answered Dec 18 '22 13:12

Isira


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

struct myClass : public QObject {
   Q_OBJECT
   // everything here is private now...
}
like image 43
virt Avatar answered Dec 18 '22 13:12

virt