|
-
April 26th, 2003, 05:23 PM
#1
No Copy Constructor Available Error
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;
}
Last edited by stober; April 26th, 2003 at 05:26 PM.
-
April 26th, 2003, 09:55 PM
#2
The copy constructor has to be const.
Code:
CCellID(const CCellID& id) {Copy(id);}
The assignment operator "=" should also take a const argument really.
Get this small utility to do basic syntax highlighting in vBulletin forums (like Codeguru) easily.
Supports C++ and VB out of the box, but can be configured for other languages.
-
April 26th, 2003, 11:40 PM
#3
Re: No Copy Constructor Available Error
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
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|