【问题标题】:Netbeans having some trouble with c++ std::map::operator[]Netbeans 在使用 c++ std::map::operator[] 时遇到了一些问题
【发布时间】:2014-10-22 11:39:33
【问题描述】:

我正在使用 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) 时,operator[] 按预期返回 Context&,但之后无法给我任何建议。 Netbeans 并不认为它是一个可以取消引用以获取 Context 字段的 Context。

编译正常 此外,“-running”被突出显示为一个一直困扰我的错误。 这种情况经常发生吗?

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

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

有什么想法吗?

【问题讨论】:

  • 顺便说一句,为什么都是星星?你打算如何删除所有这些原始指针?
  • 我写错了,还没写完就发了评论……刚刚编辑了

标签: c++ netbeans map operators


【解决方案1】:

我觉得你有

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

而不是

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

回想一下,我们将 -&gt; 用于指针,. 用于非指针。

以下

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

相当于第二个版本。您要么需要取消对指针的引用,要么按照我的首选方式考虑为什么会有这么多原始指针在运行。

我建议你改变

map<uint32_t, Context*>* contexts;

map<uint32_t, Context*> contexts;

然后您应该考虑谁拥有地图中的原始Context 指针。

【讨论】:

  • 非常感谢!是的,毫无理由地使用了太多指针......我删除了 3 个中的 2 个,它就像一个魅力。再次感谢!
  • @SOKS 很高兴修复了它 - 你可以使用指针 - 见这里 stackoverflow.com/questions/1236485/… - 我怀疑如果你这样做会导致内存泄漏
猜你喜欢
  • 2012-05-13
  • 1970-01-01
  • 2016-03-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-09-21
  • 2021-11-18
  • 1970-01-01
相关资源
最近更新 更多