Using float gives the error "calling an overloaded function is ambiguous"

I overload the function add(), but when I used the data type float, it shows an error. However, when I change it to double, it works fine. Why floatis it causing an error?

The code:

#include <iostream>
using namespace std;
class students{
    private:
        int i;
        float f;

    public:
        void add(int b){
            i=b;
            cout << "First Int: " << i;
        }
        void add(float c){
            f=c;
            cout << "Second Int: " << f;
        }

};

int main(){
    students obj;
    obj.add(9);
    obj.add(5.5);
}

Errors:

In function 'int main()':
[Error] call of overloaded 'add(double)' is ambiguous
[Note] candidates are:
[Note] void students::add(int)
[Note] void students::add(float)
+4
source share
1 answer

5.5is double, but none of your functions accept an argument double. Thus, the compiler gets confused whether to call a function with a parameter intor a function with a parameter float. So, you get an error message that is ambiguous.

, double, , , double, , , .

,

obj.add(5.5f);

f .

++

ยง 2.13.4

1 , , , e E, . ( ) . . [ : 1.602176565e-19 1.602176565e-19 . -end example] , ( ) ; , e ( E) ( ) . , . , , 10, . , , , , . literal - double, . f F float, l L double. , .

( , float )

+5

All Articles