Thread: Ambiguous operator[]

  1. #1
    Registered User MacNilly's Avatar
    Join Date
    Oct 2005
    Location
    CA, USA
    Posts
    466

    Ambiguous operator[]

    I have a class with an overloaded [] operator, as follows:

    Code:
    char & STRING::operator[] (unsigned);
    char STRING::operator[] (unsigned) const;
    The class also has a char*() operator, as follows:

    Code:
    STRING::operator char* ();
    The problem is, when I try a some code such as:
    Code:
    STRING a;
    a[0] = 'A';
    I get a compiler error saying that this operator[] call is ambiguous, that it can't decide between my STRING:perator[] and the built-in operator[] that works for char pointers. So far the only thing I can think of is that the compiler is implicitly casting my STRING to a char * using my operator char*() function. The only way this code works is if I comment out the prototype for operator char*. Unfortunately, I need to have that function working. Any help out there for me?

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Remove the conversion operator. Supply a function similar to std::string::c_str() instead. A conversion to char* is way more trouble than it is ever worth.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #3
    The larch
    Join Date
    May 2006
    Posts
    3,573
    You can be more specific when using the [] operator:
    Code:
    a.operator[](0) = 'A';
    But a better solution might be to give up the conversion overload and just use a member function to get the char*.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  4. #4
    Registered User MacNilly's Avatar
    Join Date
    Oct 2005
    Location
    CA, USA
    Posts
    466
    Thanks for the replies. I will implement it as a member function similar to string::c_str(). I guess I will have to tell the teacher that his prototypes are not going to work with one another.

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by CornedBee View Post
    Remove the conversion operator. Supply a function similar to std::string::c_str() instead. A conversion to char* is way more trouble than it is ever worth.
    QFE, although I see you've already taken this advice.

    Don't worry, everyone who ever tries to write their own string class seems to think that they can make it easier or more convenient to use than std::string, only to discover that std::string pretty much is the best the language can offer us. There is a reason for everything.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Don't worry, everyone who ever tries to write their own string class seems to think that they can make it easier or more convenient to use than std::string, only to discover that std::string pretty much is the best the language can offer us.
    std::string is not the best that the language (or standard library, rather) could have offered us. For example, it is a classic example of a monolithic class.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Help!! VC++ Problems
    By Astroboy in forum C++ Programming
    Replies: 2
    Last Post: 03-08-2004, 01:24 AM
  3. Stupid compiler errors
    By ChrisEacrett in forum C++ Programming
    Replies: 9
    Last Post: 11-30-2003, 05:44 PM
  4. Linking error
    By DockyD in forum C++ Programming
    Replies: 10
    Last Post: 01-20-2003, 05:27 AM
  5. <list>
    By Unregistered in forum C++ Programming
    Replies: 9
    Last Post: 02-24-2002, 04:07 PM