簡體   English   中英

訪問地圖時的c ++訪問沖突

[英]c++ Access violation when accessing map

我有一個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
}

您可以顯示將元素插入地圖的代碼嗎?

我試過了,它有效:

#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);
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM