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 给出错误
基础教程推荐
- 如何更改 SysDateTimePick32 或 CDateTimeCtrl 的背景颜色? 2022-01-01
- 为什么我们不能使用“虚拟继承"?在 COM 中? 2022-01-01
- 提升 ASIO 流缓冲 2021-01-01
- 将不可复制的闭包对象传递给 std::function 参数 2021-01-01
- 如何部分禁用 cmake C/C++ 自定义编译器检查 2021-01-01
- 如何在 C++ 中正确使用命名空间? 2022-01-01
- C++:获取传递给函数的多维数组的行大小 2021-01-01
- 随机插入/删除的综合向量与链表基准 2022-01-01
- c++ STL设置差异 2022-01-01
- 与 CAS 的原子交换(使用 gcc 同步内置函数) 2022-01-01
