Thread: ambiguous operator problem

  1. #1
    Registered User
    Join Date
    Mar 2002

    ambiguous operator problem

    Right now (for testing purposes), I have two functions in my class besides the default constructor:
    #1: char operator [](unsigned long iIndex) const;
    #2: operator const char *() const;

    Later on, I attempt to use the first function in a call. When I try to compile the code (posted below), the MS VC++ 6 compiler stops on the "char c=sTest[0];" line with this single error message: "error C2666: '[]' : 2 overloads have similar conversions".

    Since #1 takes 1 argument, and #2 doesn't take any arguments, I don't see how the compiler could see this as ambiguous. Removing #2 fixes the error, but I would really like to have both available in my class. Does anyone have any suggestions? I could swear I've compiled code like this on NON-MS compilers :(

    Here's the class + the code that calls it:
    class DString {
          char operator [](unsigned long iIndex) const;
          operator const char *() const; // When line removed, error goes away
          char szString[2];
       { szString[0]='X'; szString[1]='\0'; }
    char DString::operator [](unsigned long iIndex) const 
       {  return szString[iIndex]; }
    DString::operator const char *() const 
       {  return szString; }
    void main(int argc, char* argv[])
       {  DString sTest;
          char c=sTest[0];  // ERROR occurs here
          cout << "First char:" << c << endl;    
          cout << "String is:" << sTest << endl; 

  2. #2
    Registered User
    Join Date
    Aug 2001
    Compiles just fine (after changing void main to int!) with DJGPP...
    kooma -

  3. #3
    Registered User
    Join Date
    Mar 2002
    So, does this mean the the MS compiler interprets the ANSI C++ standard such that nobody can overload both the (operator []) and (operator char *) functions in the same class?!!

    That is ludicrous.

  4. #4
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    I think Stoned_Coder ran into this problem awhile ago. I had a somewhat similiar problem (made a 3d vector class, and wanted automatic conversion to float* so it would get along easier with OpenGL, but then I couldn't have an overloaded output operator). Eventually, I scrapped the float* automatic conversion. Basically, I'd recommend you do the same thing.. this way you won't have any problems with unintended conversions, and you still get the subscript operator.
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

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