CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Jun 2002
    Posts
    1,417

    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.

  2. #2
    Join Date
    Aug 2002
    Location
    Madrid
    Posts
    4,588
    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.

  3. #3
    Join Date
    Apr 1999
    Posts
    27,449

    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
  •  





Click Here to Expand Forum to Full Width

Featured