简体   繁体   中英

Problems inserting typedef in std::map

I've got a weird problem with inserting some typedefs in a simple std::map . I defined threee types:

typedef std::vector<uint8_t>                  Generation_block;
typedef std::vector<Generation_block>         Generation_blocks;
typedef std::map<uint32_t, Generation_blocks> Generations_map;

So far no errors occur. Out of this I had the idea to do it this way to reduce confusion when reading the code. Now when I want to insert some values in the map, thing get worse:

Generation_block = gen_block; //gets filled with some uint8_t data
Generation_blocks = gen_blocks; //gets filled with some Generation_block
Generations_map gen_map;

uint32_t generation_id; //gets set to several values identifiying the packet generation (for rlnc network coding purposes)
gen_map.insert(generation_id, gen_blocks); //error occurs

The last line produces the error:

error: no matching function for call to ‘std::map<unsigned int, std::vector<std::vector<unsigned char> > >::insert(uint32_t&, Generation_blocks&)’
                 gen_map.insert(gen_id, gen_blocks);

But I don't really understand what I'm doing wrong here. Does anybody have a suggestion? Is there a problem with own typedefs I just not realized out of the post ?

EDIT #1:

So I've build a minimal example:

#include<vector>
#include<cstdint>
#include<map>
#include<random>

typedef std::vector<uint8_t>                 Generation_data_block;
typedef std::vector<Generation_data_block>   Generation_blocks;
typedef std::map<uint32_t, Generation_blocks> Generations_map;

int main(){
        Generations_map gen_map;

        for(int j=0; j < 10; j++){
            Generation_blocks gen_blocks;

            for(int i = 0; i < 10; i++){
                Generation_block gen_block;

                std::generate(gen_block.begin(), gen_block.end(), rand); //generating randm data

                gen_blocks-push_back(gen_block);
            }

            uint32_t generation_id = j;

            gen_map.insert(generation_id, gen_blocks);
        }        
}
gen_map.insert(generation_id, gen_blocks);

You cannot insert elements into the std::map in this fashion.

You need to change your code to:

gen_map.insert(std::make_pair(generation_id, gen_blocks));

or, simply:

gen_map.insert({generation_id, gen_blocks});

to be compliant with std::map insert method overloads.

DEMO


In addition to that, consider changing typedefs to type aliases :

using Generation_data_block = std::vector<uint8_t>;
// ...

as it is the preferable way to do the things since C++ 11 .

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