pokche pokche - 1 year ago 80
C++ Question

Why does unordered_map say no matching constructor for initialization?

I have a struct

struct Key {
double x;
double y;
double z;

bool operator==(const Key& k) const{
return (x == k.x && y == k.y && z == k.z);

I do this because I want to make this as key for the hash map.

Then I do

std::unordered_map<Key, int> map = {{1.01, 2.02, 3.03}, 333};

and I want to use initialization list as constructor but I get error
no matching constructor for initialization of 'std::unordered_map<key, int> map = {{1.01, 2.02, 3.03}, 333};'

Answer Source

Try {{{1.01, 2.02, 3.03}, 333}} instead of {{1.01, 2.02, 3.03}, 333}

Containers can be initialized with lists of elements, not with instructions on how to construct one element.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download