Find root cause of “cannot access private member declared in class 'QObject'”

我怕爱的太早我们不能终老 提交于 2019-12-02 12:57:45

问题


I understand why I get a C2248: 'QObject::QObject' : cannot access private member declared in class 'QObject' . Qt objects are not copyable, as explained here:

  1. https://stackoverflow.com/a/3513395/356726
  2. No copy constructor or assignment operator

The problem is, that the compiler message always indicates the last line (closing }) of the class:

class MyQObject : public QObject {
       Q_OBJECT
       ....
}; <-- error line

Root cause is somewhere else, ie. where the class is copied (other file, some different line in code). This is sometimes hard to spot! Question: Is there a way to locate the line of the real reason for the error

Remark: Please note, before you mark this Duplicate. Question is about finding the root cause, not how to solve it as in the other questions.

--- Edit 1 ---

Good hint Kuba et.al. It's VS2010, compiling in Qt Creator 2.8.0

'QObject::QObject'
        C:\Qt\5.1.0-32\qtbase\include\QtCore/qobject.h(115) : see declaration of 'QObject'
        This diagnostic occurred in the compiler generated function 'MyQObject ::MyQObject (const MyQObject &)'

I wonder why a copy constructor is generated. One idea crossed my mind, I am using the DBus enabled version of Qt, might this be the reason?


回答1:


The easiest solution to detect the root cause is by making your copy ctor also private. (Or deleted, but that's not possible in VS2010 yet). This will suppress the automatically-generated copy ctor, which was the source of the error.




回答2:


If you are not explicitly copying your MyObject but you keep getting this error message then something you're using in conjunction with your MyObject is doing the copying on your behalf.

The most likely culprit would be one of the container classes, e.g. QList, QVector, etc.

Read the Container class documentation for more information as well as the specific class' documentation of any container you might be using. All containers have requirements of their elements, e.g. Must have default constructor, must be assignable, etc. This is where I think your problem lies.



来源:https://stackoverflow.com/questions/19268911/find-root-cause-of-cannot-access-private-member-declared-in-class-qobject

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!