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 给出错误
基础教程推荐
- 如果我为无符号变量分配负值会发生什么? 2022-01-01
- GDB 显示调用堆栈上函数地址的当前编译二进制文 2022-09-05
- 非静态 const 成员,不能使用默认赋值运算符 2022-10-09
- 我应该对 C++ 中的成员变量和函数参数使用相同的名称吗? 2021-01-01
- 初始化列表*参数*评估顺序 2021-01-01
- 为什么 RegOpenKeyEx() 在 Vista 64 位上返回错误代码 2021-01-01
- 为什么派生模板类不能访问基模板类的标识符? 2021-01-01
- 为什么 typeid.name() 使用 GCC 返回奇怪的字符以及如 2022-09-16
- 通过引用传递 C++ 迭代器有什么问题? 2022-01-01
- CString 到 char* 2021-01-01
