2012-09-12 49 views
2

在一個程序上工作,並且有一行代碼給我提出了問題。我不知道是否有人在這裏可以知道如何解決的這一行代碼:模糊呼叫超載 - 鑄造

long long x; 
srand(time(NULL)); 
x = rand() % 1000; 
long long range = pow (2, 60*(pow(2,x)-1)) ; 

每當我運行此我得到一個錯誤,指出有一個模糊的通話超載。 我做了一些研究,似乎與不同類型(關於long long)有關。我想可能有辦法以不同的方式來解決這個問題,但我不確定如何做到這一點。 爲了讓第二行代碼正常工作,任何人都會碰巧有什麼建議嗎?

編輯:我收到以下錯誤:

main1.cpp:149: error: ISO C++ says that these are ambiguous, even though the worst conversion for the first is better than the worst conversion for the second: 
/usr/include/architecture/i386/math.h:343: note: candidate 1: double pow(double, double) 
/usr/include/c++/4.2.1/cmath:357: note: candidate 2: float std::pow(float, float) 
main1.cpp:149: error: ISO C++ says that these are ambiguous, even though the worst conversion for the first is better than the worst conversion for the second: 
/usr/include/architecture/i386/math.h:343: note: candidate 1: double pow(double, double) 
/usr/include/c++/4.2.1/cmath:357: note: candidate 2: float std::pow(float, float) 

隨着該Luchian建議,出現以下錯誤的方法如下:

main1.cpp:149: error: call of overloaded 'pow(int, long long int&)' is ambiguous 
/usr/include/architecture/i386/math.h:343: note: candidates are: double pow(double, double) 
/usr/include/c++/4.2.1/cmath:357: note:     float std::pow(float, float) 
/usr/include/c++/4.2.1/cmath:361: note:     long double std::pow(long double, long double) 
/usr/include/c++/4.2.1/cmath:365: note:     double std::pow(double, int) 
/usr/include/c++/4.2.1/cmath:369: note:     float std::pow(float, int) 
/usr/include/c++/4.2.1/cmath:373: note:     long double std::pow(long double, int) 
+0

什麼是錯誤? http://ideone.com/GO1cT –

+1

每當我運行這個我得到「錯誤:重載調用」pow(int,long long int&)'不明確「 – Valrok

+2

編輯您的問題以包含** full **錯誤消息 - 包括候選人。 –

回答

1

二者必選其一的double版本:

long long range = ::pow (2, 60*(::pow(2,x)-1)) ; 

long long range = std::pow (2, 60*(std::pow(2,x)-1)) ; 
+0

試過這兩種方法,我得到一個稍微改變的錯誤信息。使用錯誤語句更新原始帖子,以便您可以看到。 – Valrok

+0

@朱連現在你可以投入「長雙」。 –

+0

嗯..有點困惑。我正在嘗試你的代碼的第二個版本,我需要在聲明範圍內static_cast這個?因此:「long long range = static_cast std :: pow(2,60 *(std :: pow(2,x)-1));」 – Valrok

0

我相信錯誤是pow要求第一個參數不是int

功能考生應:

(1)float pow(float base, float exp); 

(2)double pow(double base, double exp); 

(3)long double pow(long double base, long double exp); 

您需要:

long long range = pow(2.0, 60*(pow(2.0, x)-1)); 

所以編譯器可以知道使用pow

+0

http://www.cplusplus.com/reference/clibrary/cmath/pow/ –

+0

我現在雙重檢查,一秒鐘。 – Drise

+0

@LuchianGrigore另外,不要使用cplusplus。使用http://en.cppreference.com/w/。 – Drise

2

有超載的pow版本floatdouble。由於您傳遞的是long,您需要將其轉換爲floatdouble,但編譯器不知道您想要哪一個。

嘗試將2更改爲2.0。這使其成爲double,並應解決歧義。