简体   繁体   中英

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...

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. The issue is that within the loop, the insrted {key,value}replaces the previous values: keys remain but now point to the same object..

here comes my .h file:

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:

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:

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. Change the type of your map to actually store them, and you'll fix your issue.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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