Click to See Complete Forum and Search --> : No Copy Constructor Available Error


stober
April 26th, 2003, 05:23 PM
What is calusing the error shown in [COLOR=red]RED[/red] below? How to fix it?

[code]
class CCellID
{
public:
CCellID() {row = col= 0;}
CCellID(long r,long c) {row = r;col = c;}
CCellID(CCellID& id) {Copy(id);}
void Copy(CCellID& id) {row = id.row; col = id.col;}
void operator=(CCellID& id) {row = id.row; col = id.col;}
int operator==(CCellID& id) {return (row == id.row && col == id.col) ? TRUE : FALSE;}
protected:
long row,col;
};


class A
{
public:
CCellID GetCell() {return m_cell;}
[COLOR=red]error C2652: 'CCellID' : illegal copy constructor: first parameter must not be a 'CCellID'[/red]
protected:
CCellID m_cell;
}

Yves M
April 26th, 2003, 09:55 PM
The copy constructor has to be const.


CCellID(const CCellID& id) {Copy(id);}


The assignment operator "=" should also take a const argument really.

Paul McKenzie
April 26th, 2003, 11:40 PM
Also, there is nothing in your class that requires you to write a copy constructor or an assignment operator. The default ones are appropriate, given the members of the class (both are int's).

The time when you need to write a copy ctor and assignment is when you are handling pointers to dynamic memory. The class you posted does not do this.

Regards,

Paul McKenzie