Return a CStringArray gives errors(返回一个 CStringArray 给出错误)
问题描述
我试图返回一个 CStringArray:在我的.h"中,我定义了:
Im trying to return a CStringArray: In my ".h" I defined:
Private:
CStringArray array;
public:
CStringArray& GetArray();
在 .cpp 我有:
CQueue::CQueue()
{
m_hApp = 0;
m_default = NULL;
}
CQueue::~CQueue()
{
DeleteQueue();
}
CStringArray& CQueue::GetArray()
{
return array;
}
我试图从另一个文件中调用它:
From another file I'm trying to call it by:
CStringArray LastUsedDes = cqueue.GetArray();
我猜是因为上面这行,我得到了错误:
I guess it is because of the above line that I get the error:
error C2248: 'CObject::CObject' : cannot access private member declared in class 'CObject'
推荐答案
问题出在这一行
CStringArray LastUsedDes = cqueue.GetArray();
即使您在 GetArray()
函数中返回对 CStringArray
的引用,也会在上面的行中生成数组的副本.CStringArray
本身并没有定义拷贝构造函数,它派生自 CObject
,它有一个私有拷贝构造函数.
Even though you're returning a reference to the CStringArray
in the GetArray()
function a copy of the array is being made in the line above. CStringArray
itself doesn't define a copy constructor and it derives from CObject
, which has a private copy constructor.
将行改为
CStringArray& LastUsedDes = cqueue.GetArray();
但请注意,LastUsedDes
现在指的是包含在您的类实例中的相同 CStringArray
,对其中一个所做的任何更改都将在另一个中可见.
But be aware that LastUsedDes
now refers to the same CStringArray
contained in your class instance, and any changes made to one will be visible in the other.
如果您需要返回数组的本地副本,您可以使用 Append
成员函数来复制内容.
If you need a local copy of the returned array you can use the Append
member function to copy the contents.
CStringArray LastUsedDes; // default construct the array
LastUsedDes.Append( cqueue.GetArray() ); // this will copy the contents of the
// returned array to the local array
这篇关于返回一个 CStringArray 给出错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:返回一个 CStringArray 给出错误
基础教程推荐
- 从 std::cin 读取密码 2021-01-01
- 如何“在 Finder 中显示"或“在资源管理器中显 2021-01-01
- 管理共享内存应该分配多少内存?(助推) 2022-12-07
- 为 C/C++ 中的项目的 makefile 生成依赖项 2022-01-01
- 在 C++ 中循环遍历所有 Lua 全局变量 2021-01-01
- 为什么语句不能出现在命名空间范围内? 2021-01-01
- Windows Media Foundation 录制音频 2021-01-01
- 使用从字符串中提取的参数调用函数 2022-01-01
- 如何使图像调整大小以在 Qt 中缩放? 2021-01-01
- 如何在不破坏 vtbl 的情况下做相当于 memset(this, ...) 的操作? 2022-01-01