Ambiguity in operator []

Hi All,

When i try to compile the following code for 64-bit it works, whereas for 32-bit version, Compiler barfs:

#include <iostream>
#include <string>

class String {
  public:
    String() { }
    String(const char* src) : myStr(src) { }
    String(const std::string& src) : myStr(src) { }
    String(const String& src) : myStr(src.myStr) { }

    operator const char*() const         { return myStr.c_str(); }
    char& operator[](size_t index)       { return myStr[index]; }
    char operator[] (size_t index) const { return myStr[index]; }

    size_t length() const { return myStr.length(); }
  private:
    std::string myStr;
};

int main() {
    String s("abcd");

    char c = s[1];

    std::cout << c << std::endl;

    return 0;
}

$ g++ -m32 -Wall -g ambiguous.cc
ambiguous.cc: In function �int main()�:
ambiguous.cc:23: error: ISO C++ says that these are ambiguous, even though the worst conversion for the first is better than the worst conversion for the second:
ambiguous.cc:12: note: candidate 1: char& String::operator[](size_t)
ambiguous.cc:23: note: candidate 2: operator[](const char*, int) <built-in>

$ g++ -Wall -g ambiguous.cc
$./a.out
b

1) I cannot modify all the instances like, c = s[1]. There are hundreds of similar things in code.
2) operator const char* is required.

Could someone suggest any work-around, considering the above 2 points.

Thanks,
14341

---------- Post updated at 08:39 AM ---------- Previous update was at 08:38 AM ----------

Hi All,

Overloading the operator[], for all integer types (unsigned/signed) (int; long; long long) solved the problem.

Cheers,
14341