str[0] = 'a' => how to make it work (overloading operators)

This is a discussion on str[0] = 'a' => how to make it work (overloading operators) within the C++ Programming forums, part of the General Programming Boards category; Hi! I have a problem with overloading a [] operator. Whenever I want to assign value to a string I ...

  1. #1
    Code Warrior
    Join Date
    Nov 2001
    Posts
    669

    Question str[0] = 'a' => how to make it work (overloading operators)

    Hi!

    I have a problem with overloading a [] operator. Whenever I want to assign value to a string I always get this message "error C2106: '=' : left operand must be l-value".

    Example:
    str[1] = 'a';

    My code in .hpp file:
    Code:
    String &operator=(const String &secondString);
    String &operator=(const char *secondString);
    String &operator=(const char &character);
    char operator[](int index);
    Code in .cpp file:
    Code:
    String &String::operator=(const String &secondString) {
    	setString(secondString);
    	return *this;
    }
    
    String &String::operator=(const char *secondString) {
    	setString(secondString);
    	return *this;
    }
    
    String &String::operator=(const char &character) {
    	setString(character);
    	return *this;
    }
    
    char String::operator[](int index) {
    	this->setIndex(index);
    	return getChar(index);
    }
    So, what am I doing wrong?
    Current projects:
    1) User Interface Development Kit (C++)
    2) HTML SDK (C++)
    3) Classes (C++)
    4) INI Editor (Delphi)

  2. #2
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,399
    Code:
    char& operator[](int index);
    You need a reference for a l-value.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    ...otherwise your operator[] turns this statement:

    a[1] = 'a';

    into something like:

    'b' = 'a';

    which doesn't make any sense because 'b' isn't a variable. On the other hand, a reference to a char acts like a pointer to a specific location in memory, and you can assign a char to that memory.
    Last edited by 7stud; 05-06-2005 at 05:27 PM.

  4. #4
    Code Warrior
    Join Date
    Nov 2001
    Posts
    669

    Thumbs up

    Thank you very much!
    Current projects:
    1) User Interface Development Kit (C++)
    2) HTML SDK (C++)
    3) Classes (C++)
    4) INI Editor (Delphi)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 25
    Last Post: 11-30-2008, 11:30 PM
  2. overloading operators..please help
    By Tozilla in forum C++ Programming
    Replies: 5
    Last Post: 03-29-2003, 11:32 PM
  3. Purpose of Overloading Operators
    By luckygold6 in forum C++ Programming
    Replies: 3
    Last Post: 02-28-2003, 09:14 PM
  4. overloading, L-value and deference operators...
    By AznSftBaLgUrL11 in forum C++ Programming
    Replies: 3
    Last Post: 01-14-2003, 06:49 AM
  5. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 12:39 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21