1、错误代码
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
float a;
a = pow(10, 2);
cout<<"pow(10,2) = "<<a<<endl;
cin.get();
return 0;
}
报错提示:
2、错误原因
正如错误提示一样,说了pow()函数的3种形式:
long double pow(long double,int)
float pow(float,int)
double pow(double,int)
对于所给的参数int,int,编译器无法判断应该匹配哪个函数,因此报错。
需要把第一个数字转为浮点型。
3、正确代码及输出
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
float a;
a = pow((float)10, 2); //第1个数为整数会报错,需要转为浮点型。
cout<<"pow(10,2) = "<<a<<endl;
cin.get();
return 0;
}
正确输出: