6

I'm overloading the function add(), but when I used the float datatype it is showing an error. However, when I change it to double, then it's working fine. Why is float causing the error?

Code is:

#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)
Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
Asif Mushtaq
  • 3,658
  • 4
  • 44
  • 80
  • 2
    Change `obj.add(5.5);` to `obj.add(5.5f);`. – Daniel Kamil Kozar May 02 '15 at 14:38
  • 3
    5.5 is a double not a float. ``5.5f`` is a float literal. – BitTickler May 02 '15 at 14:38
  • Oooh Waoo Great, little change.. Can you little explain difference of f in float? – Asif Mushtaq May 02 '15 at 14:40
  • Since you were confused by what is happening, it should be obvious that having two methods only distinguished by the argument type, but doing different things, is a very, very bad idea. Now consider that for example size_t could be unsigned int, long, or long long on different implementations, so f (sizeof (1)) could call different methods if you do that kind of overloading! – gnasher729 Feb 20 '16 at 13:59

1 Answers1

8

5.5 is a double, but none of your functions take a double argument. So, the compiler gets confused on whether to call the function with the int parameter, or the function with the float parameter. So, you get a an error saying it is ambiguous.

That is why when you changed the function to have a double parameter, the error no longer came, because now there is a function which can take a double argument, and thus there is ambiguity there.

You can also fix the problem by calling the function as

obj.add(5.5f);

Adding the f after a number makes it to a float.

Let's look at the C++ Standard

§ 2.13.4

1 A floating literal consists of an integer part, a decimal point, a fraction part, an e or E, an optionally signed integer exponent, and an optional type suffix. The integer and fraction parts both consist of a sequence of decimal (base ten) digits. Optional separating single quotes in a digit-sequence are ignored when determining its value. [ Example: The literals 1.602’176’565e-19 and 1.602176565e-19 have the same value. —end example ] Either the integer part or the fraction part (not both) can be omitted; either the decimal point or the letter e (or E ) and the exponent (not both) can be omitted. The integer part, the optional decimal point and the optional fraction part form the significant part of the floating literal. The exponent, if present, indicates the power of 10 by which the significant part is to be scaled. If the scaled value is in the range of representable values for its type, the result is the scaled value if representable, else the larger or smaller representable value nearest the scaled value, chosen in an implementation-defined manner. The type of a floating literal is double unless explicitly specified by a suffix. The suffixes f and F specify float, the suffixes l and L specify long double. If the scaled value is not in the range of representable values for its type, the program is ill-formed.

( Sorry for posting all of it, but you can learn more about floats this way )

Daniel Kamil Kozar
  • 18,476
  • 5
  • 50
  • 64
Arun A S
  • 6,421
  • 4
  • 29
  • 43
  • Hi thanks allot problem solved.. But I'm little confused about f literal can you explain is just float required f? why double not required d? is it c++ language rule? can you give me any keyword to search about it? or any link? sorry I'm new to c++ – Asif Mushtaq May 02 '15 at 14:51
  • @AsifMushtaq , you can't convert it to a `double` with `d`, `f` for `float` is available, but no `d` for `double`. – Arun A S May 02 '15 at 14:55
  • 1
    http://en.cppreference.com/w/cpp/language/floating_literal – Alan Stokes May 02 '15 at 14:56
  • @ArunA.S Okay Got it..!! Thanks also to Alank Stokes – Asif Mushtaq May 02 '15 at 15:00
  • @AsifMushtaq , I've added a small ( okay it's quite big, but still, compared to all that given in the standard, it's quite small ) excerpt from the c++ standard. – Arun A S May 02 '15 at 15:08
  • Good @ArunA.S Got it..!! Again thanks – Asif Mushtaq May 02 '15 at 15:15