problem in overloaded functions.

can anyone assist me regarding the following program.

#include <iostream>

inline int calculate(int x, int y) { return x*y; }
inline float calculate( float a, float b) { return a*b; }

int main()
{
std::cout << calculate(12,2) << std::endl;
std::cout << calculate(12.5, 2.5);
system("PAUSE");
return 0;
}

--------------------------------------------------------
the compilar says that there is some ambiguity in overloaded function.
12.5 is a double, not a float (so is 2.5)
Change parameters as float
 
std::cout << calculate(12.5f, 2.5f);
Thank you very much both programs work.
Yes. 12.5 is double ,so if calculate(int x, int y) be called, 12.5 will be converte to int. This is standard conversions. if calculate(float x, float y) be called, 12.5 will be converte to float. This is standard conversions too.
You can change to

1
2
3
4
template <class T>
T calculate(const T& x, const T& y) {
	return x*y; 
}
Last edited on
Topic archived. No new replies allowed.