Hi, I'm trying to use the pow function. Extremely simple program like the following, but it never passed compiler:

#include <iostream>
#include <cmath>

int main()
{
int a, b, c;
a=2;
b=3;
c=pow(a, b);
cout << c;
return 0;
}
Compiler would highlight the red part. What's wrong?:icon_rolleyes: Thanks in advance.
(even fails with just "c=pow(2, 3)")

It depends on what the 'fail' is. I assume it's because (this is a fact not an assumption) the parameters for the pow() function are not integers. But I might be wrong that that's what your 'failure' is.

your compiler may require c = std::pow( a, b ) ; . this is correct as you are using <cmath> (not math.h)
the more likely reason is that the function pow is overloaded in c++

double pow ( double base, double exponent );
long double pow ( long double base, long double exponent );
float pow ( float base, float exponent );

and pow(a,b) results in an ambiguous call to overloaded function. c = std::pow( double(a), b ) ; . should remove the ambiguity.

It depends on what the 'fail' is. I assume it's because (this is a fact not an assumption) the parameters for the pow() function are not integers. But I might be wrong that that's what your 'failure' is.

Hi, thanks for the reply. When I compile it, it says error in that statement, and so the program can't run, of course no output. I tried to change "int" to "double" or "float", still to no avail. I tried on both Visual C++ and Dev C++, same thing happens. Can anyone make this simple program work on his/her computer?:?:

It might help if you said what the error message was...

your compiler may require c = std::pow( a, b ) ; . this is correct as you are using <cmath> (not math.h)
the more likely reason is that the function pow is overloaded in c++

double pow ( double base, double exponent );
long double pow ( long double base, long double exponent );
float pow ( float base, float exponent );

and pow(a,b) results in an ambiguous call to overloaded function. c = std::pow( double(a), b ) ; . should remove the ambiguity.

Thanks a lot, that does solve the problem! However why doesn't the following work: c = std::pow( int(a), int(b) ) ; i.e. it doesn't work when both a, b are "int". It works if one or both are "double".

Hi, thanks for the reply. When I compile it, it says error in that statement, and so the program can't run, of course no output.

OK... When I take my car to the fixit shop and and say "it doesn't run right" what's the first question they're going to ask me?

Please tell us every thing we need for understanding, as suggested in the post titled Read Me: Read This Before Posting. I think it's title means something :icon_wink:

OK... When I take my car to the fixit shop and and say "it doesn't run right" what's the first question they're going to ask me?

Please tell us every thing we need for understanding, as suggested in the post titled Read Me: Read This Before Posting. I think it's title means something :icon_wink:

OK, sorry, I'm newbie (both to this forum and to C++:$ ). I just read the error messages carefully and it says "call of overloaded pow(int, int) is ambiguous. Candidates are:

double std::pow(double, double)
float std::pow(float, float)
long double std::pow(long double, long double)
double std::pow(double, int)
float std::pow(float, int)
long double std::pow(long double, int)"

There's no "std::pow(int, int)", so no wonder!
Kind of annoying that such a simple thing requires so much to write, and the textbook makes no mention of the need to add "std::".

Thank you all so much for the help!

The ISO/ANSI C function is double pow( double, double ) ; C++98 functions are:

float std::pow( float, float ) ;
float std::pow ( float, int ) ;
double std::pow( double, double ) ;
double std::pow( double, int ) ;
long double std::pow( long double, long double ) ;
long double std::pow( long double, int ) ;

C++0X adds:

there shall be additional overloads sufficient to ensure that:
1. If any argument corresponding to a double parameter has type long double, then all rguments corresponding to double parameters are effectively cast to long double.
2. Otherwise, if any argument corresponding to a double parameter has type double or an integer type, then all arguments corresponding to double parameters are effectively cast to double.
3. Otherwise, all arguments corresponding to double parameters are effectively cast to float.

so these ambiguities would go away with C++0X

Thank you very much vijayan121. Nowhere would I have got the answer from the book.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.