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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With