Error initializing map

Feb 9, 2014 at 10:43pm
Okay so inside my ArcherArmor.cpp, I'm trying to figure out why the map initializer list isn't working, but for some reason I keep getting "Error C2593: 'operator =' is ambiguous". Here is my code:

I also have a class which ArcherArmor is derived from, that has a struct called `Armor`, which I left out for simplicity. The main problem is that error! The only thing I can think of is that im initializing the map wrong.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
    //ArcherArmor.h
    #include <string>
    #include <map>
    class ArcherArmor
    {
        private:
        map <int, Armor> soldier_armor;
    	public:
    	void ArcherArmor_shop();
    };
    
    //ArcherArmor.cpp
    #include "ArcherArmor.h"
    void ArcherArmor::ArcherArmor_shop(){
    	soldier_armor = {//name, damage, price
    			{1, Armor("Meito Ichimonji", 4, 150, 1)},
    			{2, Armor("Shusui", 10, 230, 2)},
    			{3, Armor("Apocalypse", 16, 300, 3)},
    			{4, Armor("Blade of Scars", 24, 550, 4)},
    			{5, Armor("Ragnarok", 32, 610, 5)},
    			{6, Armor("Eternal Darkness", 40, 690, 6)},
    			{7, Armor("Masamune", 52, 750, 7)},
    			{8, Armor("Soul Calibur", 60, 900, 8)}
    		};
    }
    
    //Main.cpp
    /*code left our for simplicity*/
Last edited on Feb 9, 2014 at 10:58pm
Feb 9, 2014 at 10:48pm
I don't see where soldier_weapons is declared.
Feb 9, 2014 at 10:52pm
oh i forgot to put it. Its in my ArcherArmor class as private.
 
private: map<int, Weapon> soldier_weapons;
Last edited on Feb 9, 2014 at 10:53pm
Feb 10, 2014 at 6:19am
Gah, you keep editing your code. It is not how it was initially.

Anyway, the issue is that you cannot use that syntax when assigning to the map - you can only use that syntax when first initializing the map:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
	void ArcherArmor::ArcherArmor_shop()
	: soldier_armor
	{
		{1, Armor("Meito Ichimonji",   4, 150, 1)},
		{2, Armor("Shusui",           10, 230, 2)},
		{3, Armor("Apocalypse",       16, 300, 3)},
		{4, Armor("Blade of Scars",   24, 550, 4)},
		{5, Armor("Ragnarok",         32, 610, 5)},
		{6, Armor("Eternal Darkness", 40, 690, 6)},
		{7, Armor("Masamune",         52, 750, 7)},
		{8, Armor("Soul Calibur",     60, 900, 8)}
	}
	{
	}
Feb 10, 2014 at 6:33am
Sorry about that, its that i have many classes like this; I have one for weapons, one for armor one for potions, etc. I was just trying to get the basic logic down. Its basically the same thing i just changed the named to better match the functions. And so what syntax will i have to use? will i have to write like this for instance:
 
soldier_armor[1] = {1, Armor("Meito Ichimonji", 4, 150, 1)};


and When you first initialize a map, isn't that assigning values to it? How do they differ?
Feb 10, 2014 at 10:51am
and When you first initialize a map, isn't that assigning values to it? How do they differ?

Initialisation is not the same as assignment, no.

Initialisation causes the entity to be created with the initial state already in place. Assignment changes the state of an entity that's already been created.
Last edited on Feb 10, 2014 at 10:51am
Feb 10, 2014 at 12:02pm
> will i have to write like this for instance:
> soldier_armor[1] = {1, Armor("Meito Ichimonji", 4, 150, 1)};

No, your original code is fine. It should use the following overload of operator=:

std::map<K,V>& operator=( std::initializer_list< std::map<K,V>::value_type > ilist );

which is the same as: std::map<K,V>& operator=( std::initializer_list< std::pair<const K,V> > ilist );

The original code would compile and run cleanly with both clang++/libc++ and g++/libstdc++:
See: http://coliru.stacked-crooked.com/a/92d689a4a084285e


However, C2593 is Microsoft. If the compiler is baulking, try:
1
2
3
4
5
6
7
8
9
10
11
soldier_armor = map <int, Armor>( 
                        {//name, damage, price
    			{1, Armor("Meito Ichimonji", 4, 150, 1)},
    			{2, Armor("Shusui", 10, 230, 2)},
    			{3, Armor("Apocalypse", 16, 300, 3)},
    			{4, Armor("Blade of Scars", 24, 550, 4)},
    			{5, Armor("Ragnarok", 32, 610, 5)},
    			{6, Armor("Eternal Darkness", 40, 690, 6)},
    			{7, Armor("Masamune", 52, 750, 7)},
    			{8, Armor("Soul Calibur", 60, 900, 8)}
    		} );

Feb 12, 2014 at 6:25pm
Amazing! t works now! Thank you!
Topic archived. No new replies allowed.