papiozo
papiozo

Reputation: 11

Again but without solution: error C2248: 'CObject::CObject' : cannot access private member declared in class 'CObject'

I write a program with my class:

class COrder
{
public:
    COrder();
    ~COrder();


public:
        ...
    CList < CItem > m_oItem;
        ...
};

which suppose to have list od object of my other class:

class CItem
{
public:
    CItem();
    ~CItem();

public:
    int m_i;
    double m_d;
    CString m_o;
};

and compiler give me error like this in title. Any ideas why ? In program I use COrder in map:

CMap <CString, LPCTSTR, COrder, COrder> m_map

Quote:

Add copy-constructor and assignment operator to your class COrder.

I add operator= to my class:

COrder& operator=( const COrder oNewOrder )
{
        ...
    m_oItem.AddTail( oNewOrder.m_oItem.GetTail() );
        ...
    return *this;
}

but what you mean by adding "copy-constructor" ?

http://msdn.microsoft.com/en-us/library/ccb3dh5c.aspx i found this but how to implement it in my code. i can't change CList class.

Upvotes: 0

Views: 1994

Answers (2)

Vishwanath Kamath
Vishwanath Kamath

Reputation: 360

http://www.codeproject.com/Articles/13458/CMap-How-to

Add copy-constructor and assignment operator to your class COrder. This makes the class copyable.

[If class is used in as Key then you need HashKey() and CompareElemenst() in that class]

Also note that STL containers are superior to MFC containers.

You get an error because CMap has default copy-ctor but CMap and CList is derived from CObject and CObject declares private copy constructor and operator=.

So, CMap doesn't offer a copy semantic "out of the box".

I would suggest you to use STL std::map container, which is designed in a way to implement copy semantic out-of-the-box.

What you don't have with STL out of the box is serialization only.

Note that std::map does not have the confusing ARG_KEY and ARG_VALUE templates. std::map just has the Key and Type template arguments (in its basic form).

http://msdn.microsoft.com/en-us/library/s44w4h2s%28VS.80%29.aspx

Or else you can go by the pointer way as Ajay suggested by which you will just shut up the compiler.

Upvotes: 1

Ajay
Ajay

Reputation: 18461

The problem statement:

CList<CItem> m_oItem;

And the trigger statement (or some usage):

CMap <CString, LPCTSTR, COrder, COrder> m_map;

Why? Well, CMap would call copy constructor and/or assignment operator for COrder. You didn't provide any, but compiler provides them from your class (i.e. for COrder). This class contains a CList object, which is inherited from CObject. CObject doesn't provide (or better say: Prevents) copy-constructor or assignment operator.

As a result, the compiler raises the error. Unfortunately, the (bad) compiler doesn't give you back-trace of this error.

Best bets for as the solution:

  • CList < CItem* > m_oItem;
  • CList<CItem> *m_poItem;
  • Use or implement your own collection.

Upvotes: 0

Related Questions