Thread: 3 Overloads Have Similar Conversions

  1. #1
    Registered User
    Join Date
    Mar 2012
    Posts
    45

    3 Overloads Have Similar Conversions

    Hi everyone,
    I overloaded the addition operator for pointer notation in my assignment and I keep getting this error when I compile:

    3 overloads have similar conversions
    could be 'char *operator +(int,const String &)'
    or 'String operator +(char,String &)'
    or 'String operator +(const String &,const String &)'
    while trying to match the argument list '(int, String)'
    note: qualification adjustment (const/volatile) may be causing the ambiguity

    It is happening at this part of the code:
    Code:
    void test15() {
        cout << "15. Testing S15: Pointer notation." << endl << endl;
        csis << "15. Testing S15: Pointer notation." << endl << endl;
        String s15("ABCDE");
        for(int i = 0; i < s15.getLength(); i++)
            ++(*(s15+i));
        for (int j = 0; j < s15.getLength(); j++) {
            cout << *(j + s15);
            csis << *(j + s15);
        }
        cout << endl;
        csis << endl;
        wait();
    }
    And these are my overloaded operators...

    I believe this is the one it should be using for the pointer notation in this case:
    Code:
    char* operator+(int index, const String& str){
        char* value;
        value = new char[1];
        value[0] = str.buf[index];
        return value;
    }
    Code:
    char* operator+(const String& str, int index){
        char* value;
        value = new char[1];
        value[0] = str.buf[index];
        return value;
    }
    The following one isn't even supposed to be used for pointer notation, but it came up in the error...
    Code:
    String operator+(char a, String& str){
        int len = str.length;
        int count = 0;
        char* temp;
        temp = new char[len];
        for(int i = 0; i < str.length; i++){
            temp[i] = str.buf[i];
            count++;
        }
        temp[count + 1] = a;
        return temp;
    }
    So this means that the compiler doesn't know which one to use right? Did I overload them incorrectly?

    Thanks for any help!

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,633
    Please post the complete error messages, these messages have important information to aid in locating and fixing the errors.

    Jim

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Marking your single-argument constructor(s) with the explicit keyword will likely solve this problem.
    Look up the explicit keyword.
    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"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. stream / bitshift overloads
    By Trauts in forum C++ Programming
    Replies: 1
    Last Post: 05-17-2003, 06:58 PM
  2. Templates AND operator overloads
    By Trauts in forum C++ Programming
    Replies: 2
    Last Post: 04-08-2003, 01:39 PM
  3. overloaded on overloads
    By emceedee in forum C++ Programming
    Replies: 1
    Last Post: 03-31-2003, 02:14 AM
  4. Operator Overloads and more?
    By Trauts in forum C++ Programming
    Replies: 1
    Last Post: 02-05-2003, 07:55 PM
  5. operator function overloads
    By The WaRped OnE in forum C++ Programming
    Replies: 1
    Last Post: 11-13-2001, 07:45 PM