Overloading functions in a class

The following code gives me this problem:
(I included math.h)
base and error are public doubles in the class

1
2
3
4
5
6
7
calc calc::log(const calc& x) 
{
	calc temp;
	temp.error = 1/x.base*x.error;
	temp.base = log(x.base);
	return temp;
}


In member function 'calc calc::log(const calc&)':
error: no matching function for call to 'calc::log(const double&)'
temp.base = log(x.base);

note candidate is:
note: calc calc::log(const calc&)
calc calc::log(const calc& x)

note: no known conversion for argument 1 from 'const double' to 'const calc&'

I have no idea why he can't find the function log in math.h and why he can't convert it.. Any help is appreciated, I hope I didn't do a rookie mistake
Last edited on
Not clear from your post if you're including using namespace std; or not.

Several possibilities:
1) It would appear that you haven't included using namespace std;. Therefore the compiler is not looking in the std:: namespace for log().

2) If you've included using namespace std;, then your member function (calc::log) takes precedence in resolving the function call. If you want the log function in <cmath>, use the global scope resolution operator (::).

 
temp.base = ::log(x.base);


3) Since you haven't shown the class declaration for calc, it's not clear what type x.base is. Since you're using <math.h>, you're getting the C declarations, not the C++ declarations for log(). They are different. If x.base is not a double, that may explain why the compiler did not find a match. The declarations for log() vary depending on whether you're using C or C++ library and by which version of the standard.

BTW, you should be using <cmath>, not <math.h>.

Edit: OP stated error and base were doubles.


Last edited on
Ah okay, thanks so much for you quick help :))
Topic archived. No new replies allowed.