问题描述
我明白为什么我得到一个 C2248:'QObject :: QObject':不能访问在'QObject'中声明的私有成员
。 Qt对象不可复制,如下所述:
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:
- http://stackoverflow.com/a/3513395/356726
- No copy constructor or assignment operator
问题是,编译器消息总是指示类的最后一行(关闭}
): / p>
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.
---编辑1 ---
--- Edit 1 ---
好提示Kuba et.al.它是VS2010,在Qt Creator 2.8.0中编译
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 &)'
我不知道为什么会生成一个复制构造函数。一个想法是我的想法,我使用的DBus启用版本的Qt,这可能是原因?
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?
推荐答案
没有明确复制你的 MyObject
,但你不断得到这个错误信息,那么你与 MyObject
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.
最可能的罪魁祸首是一个容器类,例如 QList
, QVector
等。
The most likely culprit would be one of the container classes, e.g. QList
, QVector
, etc.
a href =http://qt-project.org/doc/qt-4.8/containers.html =nofollow>容器类文档以获取更多信息以及特定类的任何文档你可能使用的容器。所有容器都具有其元件的要求,例如。必须有默认构造函数,必须可分配等。这是我认为你的问题所在。
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.
这篇关于查找“无法访问在类'QObject'中声明的私有成员的根本原因”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!