[英]'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. 尝试将函数参数中的CTypedPointerList实例从一个类发送到另一类时,会发生此错误。
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 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. 我需要确定您的代码,但是看来您正在尝试按值传递CTypedPointerList。 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. CTypedPointerList的作者已将复制构造函数标记为私有,以指示无法创建此类的副本。
Try passing by reference (perhaps a const reference?). 尝试按引用传递(也许是const引用?)。 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. GetLog()方法返回该实例的副本 ,并且由于无法复制该实例,因此无法编译该实例。 I expect what you really want to do is return a const reference to the instance. 我希望您真正想要做的是返回对该实例的const引用。 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. 为此,在h和cpp文件GetLog()
的返回类型更改为const CLogData &
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.