返回一个CStringArray给出错误

返回一个CStringArray给出错误

本文介绍了返回一个CStringArray给出错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要返回一个CStringArray:
在我的.h我定义:

  b $ b CStringArray array; 

public:
CStringArray& GetArray();

cpp我有:

  CQueue :: CQueue()
{
m_hApp = 0;
m_default = NULL;
}


CQueue ::〜CQueue()
{

DeleteQueue();
}

CStringArray& CQueue :: GetArray()
{

return array;
}



从另一个文件,我试图调用它:

  CStringArray LastUsedDes = cqueue.GetArray(); 

我想这是因为上面这行,我得到错误:

 错误C2248:'CObject :: CObject':无法访问在'CObject'类中声明的私有成员


解决方案

问题出在这一行上

  CStringArray LastUsedDes = cqueue.GetArray(); 

即使您要返回对 CStringArray GetArray()函数中,数组的副本是在上面的行。 CStringArray 本身没有定义一个拷贝构造函数,它来自于 CObject ,它有一个私有拷贝构造函数。 p>

将行更改为

  CStringArray& LastUsedDes = cqueue.GetArray(); 

但请注意, LastUsedDes 在您的类实例中包含的 CStringArray 中包含的也是一样的。



如果需要返回数组的本地副本,可以使用 Append 成员函数复制内容。

  CStringArray LastUsedDes; // default construct the array 
LastUsedDes.Append(cqueue.GetArray()); //这将把
//返回数组的内容复制到本地数组


Im trying to return a CStringArray:In my ".h" I defined:

    Private:
    CStringArray array;

    public:
    CStringArray& GetArray();

In . cpp I have:

    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'
解决方案

The problem is on this line

CStringArray LastUsedDes = cqueue.GetArray();

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.

Change the line to

CStringArray& LastUsedDes = cqueue.GetArray();

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.

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给出错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-18 20:40