nabegheh95
nabegheh95

Reputation: 305

using CArray object in CArray

i have a problem to use CArray object in CArray

// .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:

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

how do i resolve it? please help me!

Upvotes: 0

Views: 1241

Answers (3)

nabegheh95
nabegheh95

Reputation: 305

is this code ok?

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;
    };

Upvotes: 0

nabegheh95
nabegheh95

Reputation: 305

I should define “copy constructor” and “assignment operator” functions for "ArrClass" class .

class ArrClass : public CArray<int, 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, thank you all.

Upvotes: 3

Igor Tandetnik
Igor Tandetnik

Reputation: 52621

CArray requires its element type to be copyable - but it is not itself copyable. For that reason, you cannot have a CArray of CArrays.

Consider using std::vector instead.

Upvotes: 1

Related Questions