8

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'
9
  • 3
  • 2
    Are you sure that's the piece of code that's generating the error? Returning a reference to the CStringArray should work, that error usually shows up when you try to return by value.
    – Praetorian
    Commented Aug 6, 2012 at 18:11
  • 1
    @user1563551 Then please post a complete, compilable example that reproduces the error.
    – Praetorian
    Commented Aug 6, 2012 at 18:17
  • 2
    @user1563551 How can anyone tell you if anything needs to be changed in the constructor when you haven't posted the code? It is not possible to help you unless you post some code we can try compiling ourselves to figure out what is going on. Start taking stuff out of your class until it is as small as possible and still produces the same error message. Then post the class definition here along with some code showing how you're using it.
    – Praetorian
    Commented Aug 6, 2012 at 18:27
  • 1
    out of curiosity, if you have your array set to public anyway, why don't you just access the data with the dot operator. Most people will frown upon that, but hey, if you're in a hurry. Commented Aug 6, 2012 at 21:06

1 Answer 1

7

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
1
  • 1
    these Microsoft guys could have made the people's life simpler by just implementing something like public: CStringArray(const CStringArray& src) {Append(src);}
    – sergiol
    Commented Jul 16, 2021 at 1:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.