user1658000
user1658000

Reputation: 13

Conflict between math.h and cmath?

I have trouble compiling due to an "ambiguity". I have:

#include <cmath>
#include <math.h>

float q;
setActivacion(1/(1+pow(M_E,-q)));

Compiler says: ISO C++ says that these are ambiguous, even though the worst conversion for the first is better than the worst conversion for the second:

double pow(double, double) float std::pow(float, float)

I've tried plugging in straight decimal values into the arguments of pow() and it works. It just seems to have trouble taking a variable.

Thanks in advance.

Upvotes: 1

Views: 2536

Answers (3)

Steve Jessop
Steve Jessop

Reputation: 279305

The error message tells you that you should either pass in two floats, or two doubles. You are passing in a double and a float.

Try pow(M_E, static_cast<double>(-q))

For what it's worth, I can't get this error out of g++ (4.5.3). I can get a similar error out of Comeau, even if I only include one of the two headers (and regardless of which).

It may be there's some detail of gcc's implementation, that means there's only one matching version of pow. And it may be that on your compiler, including only one of the headers or specifying the namespace for pow removes the ambiguity too. But it's not usually a good idea to rely on a fix that depends on details of your implementation.

Upvotes: 1

KayEss
KayEss

Reputation: 2300

You probably have a using namespace std; in there somewhere don't you? You can also specify which version you want, i.e. using std::pow or using ::pow (syntax from memory, but hopefully you get the idea).

Upvotes: 0

user529758
user529758

Reputation:

That's because the C++ standard library incorporates the C standard library - math.h and cmath shall be identical apart from the fact that cmath puts all the stuff in the std:: namespace. So don't include both of them - if you're using C++, you should probably #include <cmath> only.

Upvotes: 2

Related Questions