Hi I am a beginner in C++ programming.

I am required to use the power function for one of my programs and keep getting the error message.

Below is my program

1 #include <iostream>
  2 #include <cmath>
  3 using namespace std;
  4 
  5 int main(){
  6     
  7     //Declare and initalize variables
  8     int i;
  9     double result;
 10     cin >> i;
 11     
 12     result = pow (10,i);
 13     
 14     cout << result << endl;
 15     
 16     return 0;
 17 }

But i keep getting
try.cpp: In function `int main()':
try.cpp:12: error: call of overloaded `pow(int, int&)' is ambiguous
/usr/include/iso/math_iso.h:63: note: candidates are: double pow(double, double)
/usr/sfw/lib/gcc/sparc-sun-solaris2.10/3.4.3/../../../../include/c++/3.4.3/cmath:361: note: long double std::pow(long double, int)
/usr/sfw/lib/gcc/sparc-sun-solaris2.10/3.4.3/../../../../include/c++/3.4.3/cmath:357: note: float std::pow(float, int)
/usr/sfw/lib/gcc/sparc-sun-solaris2.10/3.4.3/../../../../include/c++/3.4.3/cmath:353: note: double std::pow(double, int)
/usr/sfw/lib/gcc/sparc-sun-solaris2.10/3.4.3/../../../../include/c++/3.4.3/cmath:349: note: long double std::pow(long double, long double)
/usr/sfw/lib/gcc/sparc-sun-solaris2.10/3.4.3/../../../../include/c++/3.4.3/cmath:345: note: float std::pow(float, float)
as my error message. Any body would be kind enough to explain to me why ?

Recommended Answers

All 4 Replies

>power function for one of my programs and keep getting the error message.

Take a look at error description and the prototype for c++ pow().

You either need to change the type of value 10 to double (10.0) or cast it to double when you use it.

I know that pow returns a double. So whenever i use a double, Both my input must be double or only one of them ?

Another question. Can i type cast the returned value from pow to become an integer ?

Thanks :)

Check out what adatapost was trying to show you, or you can even look in your error message:

/usr/include/iso/math_iso.h:63: note: candidates are: double pow(double, double)
/usr/sfw/lib/gcc/sparc-sun-solaris2.10/3.4.3/../../../../include/c++/3.4.3/cmath:361: note:[long double std::pow(long double, int)
/usr/sfw/lib/gcc/sparc-sun-solaris2.10/3.4.3/../../../../include/c++/3.4.3/cmath:357: note: float std::pow(float, int)
/usr/sfw/lib/gcc/sparc-sun-solaris2.10/3.4.3/../../../../include/c++/3.4.3/cmath:353: note: double std::pow(double, int)
/usr/sfw/lib/gcc/sparc-sun-solaris2.10/3.4.3/../../../../include/c++/3.4.3/cmath:349: note: long double std::pow(long double, long double)
/usr/sfw/lib/gcc/sparc-sun-solaris2.10/3.4.3/../../../../include/c++/3.4.3/cmath:345: note: float std::pow(float, float)

Based on these prototypes (i.e., statement defining number and type of parameters as well as return type) you can use it with a double raised to a double or a double raised to an int (or any of the other patterns from the error message). Note that both prototypes I highlighted return a double.

You can cast it to an integer but you will lose any decimal information completely so it could mess things up later on when you go to use it. Probably better to let the precision go through to the next calculation and then you could cast that result.

working now

#include <iostream>
#include <cmath>
using namespace std;
int main()
{
 //Declare and initalize variables
	 int i;
	 double result;
	 cin >> i;
	 result = pow ((double)2,(double)i);
	 cout << result << endl;
	 return 0;
 }
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.