23
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?

Sulla
  • 7,631
  • 9
  • 45
  • 71

5 Answers5

30

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).

Caleb Huitt - cjhuitt
  • 14,785
  • 3
  • 42
  • 49
  • 3
    Thanks, that was exactly my problem, although the line copying the QObject was in another code file. (And it was hidden in a `myObject = MyObjectClass()`, too.) – Martin Hennings Jun 28 '12 at 09:56
13

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){
}
Igor Zevaka
  • 74,528
  • 26
  • 112
  • 128
  • Or have `aProjectData` default to `QMap()`, so you get a default constructor for free. – GManNickG Aug 17 '10 at 22:52
  • 1
    Good theory, but the only constructor for `QObject` is public, `QObject(QObject* parent=0)`. And changing Qt's API isn't really an option. – Mike Seymour Aug 17 '10 at 23:57
4

Adding a copy constructor to CHIProjectData class did the trick.

Brian Webster
  • 30,033
  • 48
  • 152
  • 225
Sulla
  • 7,631
  • 9
  • 45
  • 71
  • 4
    Just be aware that you're not truly copying the entirety of CHIProjectData, unless you are doing a bunch more work in the copy constructor to copy settings from the original instance's QObject parent to the new instance's QObject parent. This includes things like signal and slot connections. – Caleb Huitt - cjhuitt Aug 20 '10 at 14:26
3

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.

Romain Francois
  • 17,432
  • 3
  • 51
  • 77
Isira
  • 451
  • 5
  • 5
1

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...
}
virt
  • 409
  • 1
  • 4
  • 15