error on overload function

when i run this part of code i got llbd on xcode plus some random number... Cna someone help me out by point out what is wrong with code

1
2
3
4
5
6
7
8
9
10
11
12
13
int getValue()
{
int inputValue;
cout << "Enter an integer: ";
cin >> inputValue;
return inputValue;
}
double getValue()
{
double inputValue;
cout << "Enter a floating-point number: ";
cin >> inputValue;
return inputValue;
Did you put using namespace std or using std::cout/ using std::cin?
Last edited on
Yup I use all the basic of C++ such as #include <iostream> , using namespace...
is error because of same return type?
Does your code actually compile?
It shouldn't.

You can't overload functions only by return type -- the parameters have to be different.
i got it now!! Thank you
Try using different data type for input parameter even if you don't use it. See if that helps.
int getValue(int a)
{
int inputValue;
cout << "Enter an integer: ";
cin >> inputValue;
return inputValue;
}
double getValue( double a)
{
double inputValue;
cout << "Enter a floating-point number: ";
cin >> inputValue;
return inputValue;
Eh, I don't think that's what parameters are meant for.

If you really want to be able to use getValue for multiple data types, you could just pass the variable in as a reference:
1
2
3
4
5
6
7
8
9
10
void getValue(int& num)
{
    std::cout << "Enter an integer: ";
    std::cin >> num;
}
void getValue(double& num)
{
    std::cout << "Enter a floating-point number: ";
    std::cin >> num;
}

(or use templates, if you know anything about those)
Topic archived. No new replies allowed.