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 给出错误


基础教程推荐
- 常量变量在标题中不起作用 2021-01-01
- C++结构和函数声明。为什么它不能编译? 2022-11-07
- 静态库、静态链接动态库和动态链接动态库的 .lib 文件里面是什么? 2021-01-01
- 如何通过C程序打开命令提示符Cmd 2022-12-09
- 如何将 std::pair 的排序 std::list 转换为 std::map 2022-01-01
- 如何在 C++ 中初始化静态常量成员? 2022-01-01
- 如何检查GTK+3.0中的小部件类型? 2022-11-30
- 我有静态或动态 boost 库吗? 2021-01-01
- 在 C++ 中计算滚动/移动平均值 2021-01-01
- 这个宏可以转换成函数吗? 2022-01-01