Sign in to follow this  
NUCLEAR RABBIT

Function Overloading

Recommended Posts

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) 

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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 :/

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this