[問題] 解讀錯誤訊息

看板C_and_CPP (C/C++)作者 (街頭魂!!)時間13年前 (2012/07/24 22:46), 編輯推噓0(006)
留言6則, 4人參與, 最新討論串1/1
先附上程式碼 #include <iostream> #include <math.h> using namespace std; int main() { int i = 0; int max = pow(2,4); int end; double x[10]; while(i < 10) {x[i] = (rand()%max) + 1; x[i] = x[i] / max; cout << x[i] << endl; i++; } cin >> end; return 0; } 主要是在練習產生十個0~1之間的隨機亂數 然而卻產生下列的錯誤訊息: Error 1 error C2668: 'pow' : ambiguous call to overloaded function c:\documents and settings\acer\my documents\visual studio 2010\projects\practice\practice\source1.cpp 8 1 Practice 2 IntelliSense: more than one instance of overloaded function "pow" matches the argument list: c:\documents and settings\acer\my documents\visual studio 2010\projects\practice\practice\source1.cpp 8 12 Practice 不太清楚程式碼哪裡打錯了 有請板上大神幫忙抓蟲 THX~~ -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 111.250.67.52

07/24 22:53, , 1F
在VC裡pow似乎沒有整數版本
07/24 22:53, 1F

07/24 22:53, , 2F
這就要提到cmath跟math.h的差異了,在pow()上有差喔
07/24 22:53, 2F

07/24 22:54, , 3F
可以在常數後綴"."小數點轉型成double
07/24 22:54, 3F

07/24 22:57, , 4F
或是* 1.0
07/24 22:57, 4F

07/24 23:01, , 5F
pow(2.,4)
07/24 23:01, 5F

07/24 23:04, , 6F
跳 tone, max 不是個好的變數名稱。
07/24 23:04, 6F
文章代碼(AID): #1G3hLJA1 (C_and_CPP)
文章代碼(AID): #1G3hLJA1 (C_and_CPP)