【问题标题】:c++ Access violation when accessing mapc++访问map时访问冲突
【发布时间】:2014-08-10 07:00:50
【问题描述】:

我有一个map<string, std::function<void(AgentMessage&)>>(AgentMessage 是一个带有几个字符串的结构)。当我尝试使用迭代器访问它时,pair 的复制函数出现访问冲突。

注意:std::function 指向与复制位置不同的 dll 中的函数。

编辑:我认为解释对于一段简单的代码来说已经足够了,但仍然 - 就是这样。

for (map<string, std::function<void(AgentMessage&)>>::iterator it = mapRef.begin(); it != mapRef.end(); it++)
{
    auto functionCopy = it->second; // IT CRASHES HERE
}

【问题讨论】:

  • 与其描述你的代码,为什么不展示它呢? (最好是 SSCCE,sscce.org
  • 如果将函数替换为不执行任何操作的 lambda,它是否也会崩溃?

标签: c++ function access-violation functor


【解决方案1】:

你能展示将元素插入地图的代码吗?

我试过了,效果很好:

#include <functional>
#include <map>
#include <string>

using namespace std;

struct AgentMessage
{

};

void f(AgentMessage& am)
{

}

void g(AgentMessage& am)
{

}

int main()
{
    AgentMessage am;
    map<string, std::function<void(AgentMessage&)>> m;

    m["f"] = f;
    m["g"] = g;

    for (map<string, std::function<void(AgentMessage&)>>::iterator it = m.begin(); it != m.end(); ++it)
    {
        auto func = it->second;
        func(am);
    }
}

【讨论】:

  • 这并不能真正回答问题。
猜你喜欢
  • 2018-04-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-07-08
相关资源
最近更新 更多