Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why cant I insert a struct value in an unordered_map C++

I have created a very simple example to show the problem:

#include <unordered_map>

int main() {
    struct Example {
        int num;
        float decimal;
    };

    std::unordered_map<int, Example> map;
    map.insert(1, { 2, 3.4 }); // Error none of the overloads match!
}

I should be able to insert into the map of int and struct Example but the compiler says none of the overloads match..?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

The reason for the error message is, that none of the overloads of std::unordered_map::insert takes a key and a value parameter.

You should do

map.insert({1, { 2, 3.4 }});

instead of

map.insert(1, { 2, 3.4 });

You may refer to the 6th overload of std::unordered_map::insert at
https://en.cppreference.com/w/cpp/container/unordered_map/insert

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading