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:
Code:class DString { public: DString(); char operator [](unsigned long iIndex) const; operator const char *() const; // When line removed, error goes away protected: char szString[2]; }; DString::DString() { 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; }



LinkBack URL
About LinkBacks


