1

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?

Community
  • 1
  • 1
Horst Walter
  • 13,663
  • 32
  • 126
  • 228
  • Which version of VS? Mine (VS2010) reports the error on the line of usage and then, on additional lines of the report, says "see declaration of `QObject::QObject`" (which points into `QObject`). – Angew is no longer proud of SO Oct 09 '13 at 10:14
  • 1
    Don't you miss the `;` after the closing brecket `}` of your class definition? – vahancho Oct 09 '13 at 10:34
  • 2
    Humor us and post the entire error message, along with your version of visual studio. Be careful about where you get the message from - the issue list from VS IDE is *not* what you want. You want to look at it in the raw compiler output. At least on Qt Creator, the issue list will expand when you click on an issue, giving you more insight into what happened. – Kuba hasn't forgotten Monica Oct 09 '13 at 10:47
  • Post some (preferably minimal) code which reproduces the issue. – Frerich Raabe Oct 09 '13 at 10:58

2 Answers2

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.

MSalters
  • 173,980
  • 10
  • 155
  • 350
1

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.

RobbieE
  • 4,280
  • 3
  • 22
  • 36