简体   繁体   English

地图插入将替换先前输入的值

[英]map insertions replaces previously entered values

I'm trying to code a container for a user-defined class. 我正在尝试为用户定义的类编写一个容器。 I read the key from a file and construct and insert the element in a map.but when I do so in a loop, the elements are present but they all point to the same object... 我从文件中读取密钥并将其构造并插入到map中,但是当我在循环中这样做时,虽然存在这些元素但它们都指向同一个对象...

as you will see in the code, I create a "Capture" that reads from a file to instanciate "Flux"es and store them in a map. 正如您将在代码中看到的那样,我创建了一个“捕获”,该“捕获”从文件读取以实例化“ Flux”并将它们存储在地图中。 The issue is that within the loop, the insrted {key,value}replaces the previous values: keys remain but now point to the same object.. 问题在于,在循环内,插入的{key,value}会替换以前的值:保留键,但现在指向同一对象。

here comes my .h file: 这是我的.h文件:

class Flux;

class Capture
{
public:
    Capture(pcpp::PcapLiveDevice* dev, std::string pcap_prefix, unsigned int max_pcap_size = 0);
    ~Capture();

private:
    std::map<std::string,Flux&> m_list_flux;
};

class Flux
{
public:
    Flux(std::string filter,std::string folder, std::string prefix, unsigned int max_pcap_size);
    ~Flux();

    void print();

private:
    std::string m_folder;
};

and here is my .cpp: 这是我的.cpp:

Capture::Capture(pcpp::PcapLiveDevice * dev, std::string pcap_prefix, unsigned int max_pcap_size) 
{
    std::ifstream conf("flux.conf");
    std::string line;
    if (conf) {
        std::vector<std::string> cont;
        while (getline(conf, line)) {
            boost::split(cont, line, boost::is_any_of(":"));
            std::cout << "creating directory and flux: " << cont.back() << std::endl;
            fs::create_directory(cont.front());
            Flux flux(cont.back(), cont.front(), pcap_prefix, max_pcap_size);
            m_list_flux.emplace(cont.back(), flux);  //here the inserted flux replaces the previous one: the key remains intact but the value is changed: i have several keys pointing to the same object...
        }
        fs::create_directory("flux0");
        Flux flux2("any", "flux0", pcap_prefix, max_pcap_size);
        m_list_flux.emplace("any", flux2); // same here
    }

for (auto&it : m_list_flux) {
        std::cout << "launching flux: " << it.first << std::endl;
        it.second.print();
    }

}

This prints: 打印:

launching flux: 10.10.10.10
flux0
launching flux: 10.10.10.102
flux0
launching flux: any
flux0

I've tried to do it manually : 我尝试手动进行操作:

Capture::Capture(pcpp::PcapLiveDevice * dev, std::string pcap_prefix, unsigned int max_pcap_size) 
{
    Flux flux("10.10.10.10", "flux1", m_pcap_prefix, max_pcap_size);
    m_list_flux.emplace("flux1", flux);
    Flux test("10.10.10.102", "flux2", m_pcap_prefix, max_pcap_size);
    m_list_flux.emplace("flux2", test);
    Flux test2("any", "flux0", m_pcap_prefix, max_pcap_size);
    m_list_flux.emplace("flux0", test2);

    for (auto&it : m_list_flux) {
        std::cout << "launching flux: " << it.first << std::endl;
        it.second.print();
    }
}

and it works fine in that case: it prints: 在这种情况下,它可以正常工作:打印:

launching flux: 10.10.10.10
flux1
launching flux: 10.10.10.102
flux2
launching flux: any
flux0

my conf file only contains: 我的conf文件仅包含:

flux1:10.10.10.10
flux2:10.10.10.102

I'm probably missing something obvious but I'm far from being an expert so any help is appreciated. 我可能缺少明显的东西,但我远非专家,因此能提供任何帮助。 Can someone explain to me this behavior? 有人可以向我解释这种行为吗? I just want my map to contain the different flux described in my configuration file. 我只希望地图包含配置文件中描述的不同通量。

Flux flux(cont.back(), cont.front(), pcap_prefix, max_pcap_size);
m_list_flux.emplace(cont.back(), flux);  

You've commented on the second line: 您已在第二行发表评论:

here the inserted flux replaces the previous one: the key remains intact but the value is changed: i have several keys pointing to the same object... 这里插入的助焊剂代替了前一个:按键保持不变,但值已更改:我有几个按键指向同一个对象...

You're lucky (unlucky?) that this works at all. 您很幸运(不幸吗?)这一切都奏效。 You're storing references to temporary Flux objects created in that function. 您正在存储对该函数中创建的临时Flux对象的引用。 Change the type of your map to actually store them, and you'll fix your issue. 更改map的类型以实际存储它们,即可解决问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM