2014-10-22 93 views
0

我正在使用Netbeans IDE 8.0,並且遇到了一些麻煩。Netbeans在C++中遇到了一些麻煩std :: map :: operator []

這裏是代碼說明問題的程序塊

typedef struct Context { 
    bool finished; 
    bool reliable; 
    bool running; 
    bool firstAckReceived; 
    map<uint32_t, uint32_t>* missingChunks; 
} Context; 

map<uint32_t, Context*>* contexts; 
... 
this->contexts->operator[]((uint32_t) ctrl.getSource())->running = true; 

當使用建議(Ctrl +空格鍵),操作員[]返回一個上下文&符合市場預期,但畢竟不能給我任何建議那。 Netbeans並不認爲它是一個可以取消引用來獲取Context字段的Context。

編譯正常 此外,「-running」突出顯示爲一個令我困擾的錯誤。 這是否發生了很多?

另外,我想我應該可以如下,但G ++抱怨訪問使用語法元素...

this->contexts[(uint32_t) ctrl.getSource()]->running = true; 

任何想法?

+3

在附註中,爲什麼所有的星星?你打算如何刪除所有這些原始指針? – doctorlove 2014-10-22 11:43:34

+0

我犯了一個錯誤,並在發佈之前發佈了評論......剛編輯 – SOKS 2014-10-22 11:45:57

回答

0

我覺得你有

this->contexts->operator[]((uint32_t) ctrl.getSource())->running = true; 
//   ^------------- notice this 

而不是

this->contexts.operator[]((uint32_t) ctrl.getSource())->running = true; 
//   ^------------- notice this 

回想一下我們使用->爲指針和.非指針。

以下

this->contexts[(uint32_t) ctrl.getSource()]->running = true; 

相當於第二個版本。你或者需要解引用指針,或者我喜歡的方式,考慮爲什麼你有這麼多的指針正在進行。

我建議你換

map<uint32_t, Context*>* contexts; 

map<uint32_t, Context*> contexts; 

然後你應該考慮誰擁有該地圖的原始Context指針。

+0

非常感謝! 是太多的指針被用於絕對沒有理由... 我刪除了2 ou 3,它的作品像一個魅力。 再次感謝! – SOKS 2014-10-22 12:09:22

+0

@SOKS很高興修復它 - 你可以使用指針 - 見這裏http://stackoverflow.com/questions/1236485/how-to-access-elements-of-ac-map-from-a-pointer - 位I懷疑你會得到內存泄漏 – doctorlove 2014-10-22 12:20:52