2010-03-25 22 views
-1

如何解決此錯誤?對重載函數的模糊調用(const變量)

錯誤C2668: '的std :: _樹< _Traits> ::結束':以重載函數調用曖昧

我的代碼如下所示:

typedef map<int const *, float> my_map_t; 
my_map_t _test; 
my_map_t::const_iterator not_found = my_map_t::end(); 
if (_test.find(&iKeyValue) == not_found) { 
    _test[iKeyValue] = 4 + 5; // not the actual code, but here for simplicity 
} 

編譯器抱怨對my_map_t :: end()有一個模糊的調用。這是有道理的,因爲唯一的區別是返回類型。

正常情況下,您可以通過投射參數來消除歧義,但end()沒有參數。有任何想法嗎?

+3

它看起來像' _test'是一個類型,而不是一個實例(你已經'typedef''它!)。另外,什麼是'my_map_t'?它是否採用模板化參數(在這種情況下,'my_map_t :: const_iterator'沒有意義)或者沒有(在這種情況下,您的'typedef'是錯誤的)? – 2010-03-25 01:10:41

+0

你說得對。我急匆匆地打了那個片段。我糾正了,所以現在它是有道理的。我將my_map_t :: end()留在那裏,以顯示我的思路錯誤。 – Joe 2010-03-25 01:19:42

+0

當你沒有發佈你的實際代碼時,我們應該如何回答你的問題?不要「匆忙輸入片段」。根本不要輸入片段* *。向我們展示您的*實際*代碼,或者您已驗證的代碼包含相同的問題。引入新錯誤,錯別字和錯誤的代碼只會讓我們很難確定哪些錯誤是我們應該關注的。 – jalf 2010-03-25 02:19:43

回答

2

從您的代碼看來,my_map_t::end()是靜態的(否則您必須在實例上調用它,例如_test.end())。 編輯:Jesse Beder在他對這個問題的評論中是正確的。代碼沒有什麼意義,因爲_test是一種類型,而不是對象。

靜態成員函數不能是const限定的(成員函數的const限定適用於this指針;靜態成員函數沒有this指針)。

+0

D'oh!謝謝,我知道我忘了一些東西。 – Joe 2010-03-25 01:13:18

0

我不知道這是否說,正好,你的問題,因爲我是如何解釋的問題,但是這可能是幫助有點不清楚......

// BindingProblem.cpp : Defines the entry point for the console application. 
// 

#include "stdafx.h" 
#include <map> 

typedef std::map<int*, float> my_map_t; 

int _tmain(int argc, _TCHAR* argv[]) 
{ 
    int iKeyValue; 

    my_map_t _test; 

    my_map_t::const_iterator not_found = _test.end(); 

    if (_test.find(&iKeyValue) == not_found) 
    { 
     _test[&iKeyValue] = 4 + 5; // not the actual code, but here for simplicity 
    } 

    return 0; 
}