Returning CStringArray gives errors

I am 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:

    CStringArray LastUsedDes = cqueue.GetArray();

I assume that due to the above line, I get an error:

   error C2248: 'CObject::CObject' : cannot access private member declared in class 'CObject'
+5
source share
1 answer

The problem is in this line.

CStringArray LastUsedDes = cqueue.GetArray();

Even if you return a reference to CStringArrayin a function GetArray(), a copy of the array is executed in the line above. CStringArrayIt does not define the copy constructor itself, and it is obtained from CObject, which has a private copy constructor.

Change the line to

CStringArray& LastUsedDes = cqueue.GetArray();

, LastUsedDes CStringArray, , , , .

, Append .

CStringArray LastUsedDes;                // default construct the array
LastUsedDes.Append( cqueue.GetArray() ); // this will copy the contents of the
                                         // returned array to the local array
+4

All Articles