Function Overloading

Started by
2 comments, last by Shai 17 years, 9 months ago
Hello, Im practicing function overloading and i have an error i cant seem to figure out what is going wrong! Please help me [sad] Source:

#include <iostream>
using namespace std;

int askNumber(int high, int low = 1);
float askNumber(float highNum, float lowNum = 1.0);

int main()
{
    int number1 = askNumber(10);
    cout << "Thank you for entering " << number1 << "\n\n";
    
    float number2 = askNumber(10.9);
    cout << "Thank you for entering " << number2 << endl << endl; 
    
    system("pause");
    return 0;
}

int askNumber(int high, int low)
{
    int iNum;
    
    do {
        cout << "Enter a number between " << high << " - " << low << ": ";
        cin >> iNum;
    }while(iNum > low || iNum < high);
    
    return iNum;
}

float askNumber(float highNum, float lowNum)
{
    float fNum;
    
    do {
        cout << "Enter a number between " << highNum << " - " << lowNum << ": ";
        cin >> fNum;
    }while(fNum > lowNum || fNum < highNum);
    
    return fNum;
}

Errorsi get:

26 C:\Documents and Settings\Brandon\Desktop\Stuff\Programming\Book Excersises\Ch 5\5.3 - Overloaded Functions\Main.cpp call of overloaded `askNumber(double)' is ambiguous 

note C:\Documents and Settings\Brandon\Desktop\Stuff\Programming\Book Excersises\Ch 5\5.3 - Overloaded Functions\Main.cpp:18 candidates are: int askNumber(int, int) 

note C:\Documents and Settings\Brandon\Desktop\Stuff\Programming\Book Excersises\Ch 5\5.3 - Overloaded Functions\Main.cpp:18                 float askNumber(float, float) 

Advertisement

Quote:

askNumber(10.9);



I could say that this line at least produces a problem. Number 10.9 is presented as a double and could be converted to float or int (the compiler won't guess what you mean here).

writing

askNumber((float)10.9);

or

askNumber(10.9f);

might fix it.
Quote:Original post by Demus79

Quote:

askNumber(10.9);



I could say that this line at least produces a problem. Number 10.9 is presented as a double and could be converted to float or int (the compiler won't guess what you mean here).

writing

askNumber((float)10.9);

or

askNumber(10.9f);

might fix it.


Thank you very much! [smile]
My book never tought e how to fix this kind of thing.
int number1 = askNumber(10);

The integer literal 10 is stored as an integer. Therefore when this code is executed, it'll call "int askNumber(int high, int low = 1);".

float number2 = askNumber(10.9);

The floating-point literal 10.9 is stored as a double. Therefore neither "int askNumber(int high, int low = 1);" or "float askNumber(float highNum, float lowNum = 1.0);" can be called. The compiler however can implicitly convert the double to both an integer and a floating point. However it is unsure which one is desired and therefores generates an error message.

The easiest way to solve this is by writing "10.9f" rather than "10" when calling "int askNumber(float)".


EDIT: too slow :/
"It's better to regret something you've done than to regret something you haven't done."

This topic is closed to new replies.

Advertisement