'CObject::CObject' : 无法访问类 'CObject'd:\program files\microsoft Visual Studio 9.0\vc\atlmfc\include\afxcoll.h 中声明的私有成员
当尝试将函数参数中的 CTypedPointerList 实例从一个类发送到另一类时,会发生此错误。
如何解决这个问题?
这是我的代码
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;
}
问候,
karthik
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我需要查看您的代码才能确定,但看起来您正在尝试按值传递 CTypedPointerList 。这意味着需要创建实例的副本,因此需要隐式调用复制构造函数。 CTypedPointerList 的作者已将复制构造函数标记为私有,以表明无法创建此类的副本。
尝试通过引用传递(也许是 const 引用?)。如果您确实需要副本,则可能需要手动执行此操作。
编辑
啊...您正在使用实例作为返回值。 GetLog() 方法返回实例的副本,并且由于无法复制实例,因此无法编译。我希望您真正想要做的是返回对该实例的 const 引用。这意味着客户端将获得对日志的只读引用,而不进行复制。要实现此目的,请在 h 和 cpp 文件中将
GetLog()
的返回类型更改为const CLogData &
。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()
toconst CLogData &
in both the h and cpp files.