0
#include <iostream> 
using namespace std;

void pri(int x);
void pri(float x);

int main() 
{     
    float x = 10.2;     
    pri(10.2);
}

void pri(int x) {
    cout << "int" << endl; 
}

void pri(float x) {  
    cout << "float" << endl;
}

In the above code when 10.2 is given as the argument, it gives a compile error and when it is given x as the argument that is also the 10.2, it works fine. What is happening here?

LogicStuff
  • 19,397
  • 6
  • 54
  • 74
Erangad
  • 671
  • 7
  • 16
  • 1
    What's the exact, full error message? I'm pretty sure it does give you a hint what's wrong! Also, what behaviour would you expect? And why? – Ulrich Eckhardt Feb 20 '16 at 13:13
  • 4
    10.2 is a double, 10.2f is a float. – Daniel Feb 20 '16 at 13:14
  • 2
    It helps if you include what errors you get. Could it be that `10.2` will be a double and the compiler doesn't know which overload to implicitly cast to? You might want to try `10.2f`. – thomthom Feb 20 '16 at 13:14
  • There's no preferred narrowing conversion between double-to-int and double-to-float, so the call is ambiguous. The initialization of `x` is of course not ambiguous. – Kerrek SB Feb 20 '16 at 13:14

0 Answers0