Thread: what goes wrong with the operator=()?

  1. #1
    Registered User
    Join Date
    Sep 2001
    Posts
    4

    Angry what goes wrong with the operator=()?

    hi, I am writing code for output data from a BST node. the node is a Client class consist of ID, name and address. when the searching ID is matched, then the data of the client is printed. there is a operator=() overloading in the BST header file, like this:
    class client {
    public:
    ...
    void operator=(client const &);
    ..
    char *GetName() { return Name;}
    char *GetPhone() {return Phone;}
    int GetID(){ return ID;}
    ...
    private:
    char *Name;
    char *Phone;
    int ID;
    };

    void client:perator=(cleint const &person)
    {
    Name=person.GetName();
    Phone=person.GetPhone();
    ID=person.GetID();
    }

    I also tried in this way:
    void client:perator=(cleint const &person)
    {
    strcpy( Name, person.GetName()) ;
    strcpy( Phone, person.GetPhone());
    ID=person.GetID();
    }
    but when I compile the code there is always an error message:

    error C2662: 'GetName' : cannot convert 'this' pointer from 'const class client' to 'class client &'
    .

    what goes wrong with the code? I use VC++, could some one help me to figure out the problem?

    thanks in advance!


  2. #2
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    You'll have to make your accessor functions consts (only const member functions can be called for const objects) -

    char *GetName()const { return Name;}
    char *GetPhone()const {return Phone;}
    int GetID()const { return ID;}

    and then your first version should work (const client& person).
    zen

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Debugging-Looking in the wrong places
    By JaWiB in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 11-03-2003, 10:50 PM
  2. Copy constructors and operator=()
    By filler_bunny in forum C++ Programming
    Replies: 13
    Last Post: 08-25-2003, 07:43 AM
  3. Confused: What is wrong with void??
    By Machewy in forum C++ Programming
    Replies: 19
    Last Post: 04-15-2003, 12:40 PM
  4. God
    By datainjector in forum A Brief History of Cprogramming.com
    Replies: 746
    Last Post: 12-22-2002, 12:01 PM
  5. Whats wrong?
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 07-14-2002, 01:04 PM