Netbeans在使用c++std::map::operator[]时遇到一些问题

Netbeans having some trouble with c++ std::map::operator[]

本文关键字:遇到 问题 operator c++std map Netbeans      更新时间:2023-10-16

我正在使用Netbeans IDE 8.0,但遇到了一些问题。

下面是一段解释问题的代码:

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+Space)时,运算符[]返回Context&正如预期的那样,但之后不能给我任何建议。Netbeans并不认为它是一个可以被取消引用以获取Context字段的Context。

编译可以此外,"跑步"被强调为一个一直困扰着我的错误。这种情况经常发生吗?

另外,我认为我应该能够使用如下语法访问元素,但g++抱怨。。。

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

有什么想法吗?

我想你有

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指针。