简体   繁体   中英

'CObject::CObject' : cannot access private member declared in class 'CObject'd:\program files\microsoft visual studio 9.0\vc\atlmfc\include\afxcoll.h

This error happens while trying to send the instance of CTypedPointerList in the function parameter from one class to another class.

How to solve this issue?

Here is my code

ObjectList.h

#pragma once
#include "LogData.h"
typedef  CTypedPtrArray<CPtrList , CLog *> CLogData;
class CObjectList
{
    public:

CLogData m_logData;
    public:
CObjectList();
CLogData GetLog();
};

ObjectList.cpp

#include "stdafx.h"
#include "LogData.h"

CObjectList::CObjectList()
{
}

CLogData CObjectList::GetLog()
{
return m_logData;
}

Regards,

karthik

I'd need to see your code to be sure, but it looks like you're trying to pass the CTypedPointerList by value. This means a copy of the instance needs to be created, thus the implicit call to the copy constructor. The authors of CTypedPointerList have marked the copy constructor private in order to indicate that copies of this class cannot be made.

Try passing by reference (perhaps a const reference?). If you truly do need a copy, you may need to do this manually.

EDIT

Ahh... you're using the instance as a return value. The GetLog() method returns a copy of the instance, and since the instance cannot be copied, it does not compile. I expect what you really want to do is return a const reference to the instance. This means client will get a read-only reference to the log, no copy is made. To achieve this, change the return type of GetLog() to const CLogData & in both the h and cpp files.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM