function overloading in C++ not working when parameters differ by type

87 views Asked by At

I know that overloaded functions in C++ can be differentiated by the number parameters, type and sequence as it says here.

However, when I tried the following code, I've got an ambiguity error but I don't know why since one of the parameters differs (and the sequence should not matter in this case):

class MyClass
{
    public:

    MyClass() {}

    void myMethod(char x, int y){
        cout<<"int"<<endl;
    }

    void myMethod(char x, float y){ //if the arguments are swapped, the function is not ambiguous anymore
        cout<<"float"<<endl;
    }
};

int main()
{
    MyClass obj;
    obj.myMethod('x', 1);
    obj.myMethod('x', 0.5);
}

0

There are 0 answers