2012-05-06 17 views
2
static const map<const Type*, int>* priority; 

其中Type是我自己的特殊類。爲什麼C++告訴我沒有[]匹配我傳入的類型?

初始化(C++爲什麼要求這個我不知道,汽車甚至不工作)

const map<const Type*, int, less<const Type*>, allocator<pair<const Type* const, int>>>* ToolProperty::priority 
= new map<const Type*, int, less<const Type*>, allocator<pair<const Type* const, int>>>(); 

最後,嘗試使用它(它告訴我,我傳遞了錯誤的型)

static void setPriority(const Type* type, int newPriority) 
{ 
    (*priority)[type] = newPriority; 
} 
+0

C++是一種編程語言,它不會告訴你的東西。你的編譯器在說什麼? (例如,你在使用什麼編譯器,以及你得到的錯誤或警告是什麼?) – dreamlax

+2

爲什麼你使用這麼多的指針?這似乎有點代碼味道。 –

+1

如果所有你想要的都是默認值,沒有理由說出比較器和分配器。把它們留下。 –

回答

5

優先是一個指向一個const地圖,但操作符[]不能與const associative containers使用。

+0

謝謝,它修復了=) – nestharus

+0

@ user1136671 operator []不能與const一起使用,因爲如果它的參數尚不在地圖中,它實際上會在地圖中插入一個新元素。 – Matthias

相關問題