返回一个 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
相关文章