Click here to Skip to main content
15,885,720 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi, i have a problem to use CArray object in CArray


C#
// .h file
class ArrClass : public CArray<int, int>
{
public:
    int m_id;
    void Func1(){ m_id = 1;};
};

// .cpp file
void CTestDlg::OnBnClickedButton1()
{
	ArrClass arr1;
	CArray<ArrClass, ArrClass> arr2;
	arr2.Add(arr1);     // error !!!! 
}


this error is:
VB
error C2248: 'CObject::CObject' : cannot access private member declared in class 'CObject'


how do i resolve it? please help me!
Posted

because CArray requires its element type to be copyable, i should define “copy constructor” and “assignment operator” functions for "ArrClass" class.

class ArrClass : public CArray<int,>
{
public:
    int m_id;

    ArrClass(){};
    ArrClass(const ArrClass& obj){m_id = obj.m_id;};    // copy constructor
    void Func1(){ m_id = 1;};
    ArrClass operator=(const ArrClass& obj) // assignment operator
    {
        m_id = obj.m_id;
        return *this;
    };
};


the error resolved.
 
Share this answer
 
Comments
Zon-cpp 5-Aug-14 1:10am    
I found a question: copy constructor and assignment operator only copy over m_id member - but not the actual contents of the CArray. It will always be empty (as in, GetSize() will return 0)
I think this code is ok.

C++
ArrClass(const ArrClass& obj)   // copy constructor
    {
        Copy(obj);

        m_id = obj.m_id;
    };  

ArrClass operator=(const ArrClass& obj) // assignment operator
    {
        Copy(obj);

        m_id = obj.m_id;
        return *this;
    };


there is other solution?
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900