1. 程式人生 > >C++的error C2668: 'pow' : ambiguous call to overloaded function錯誤原因及解決方法

C++的error C2668: 'pow' : ambiguous call to overloaded function錯誤原因及解決方法

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;
}

正確輸出: