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.

#include <string>
#include <map>
class ArcherArmor
    map <int, Armor> soldier_armor;
    void ArcherArmor_shop();

#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)}

/*code left our for simplicity*/

Edited by Damian_2

3 Years
Discussion Span
Last Post by Damian_2

I don't think you can initialise a std::map like that.

You can initialise std::map in a similar way using map_list_of in the boost::assign library.
Which would allow you to do something like this (if memory serves):

void ArcherArmor::ArcherArmor_shop(){
    soldier_armor = boost::assign::map_list_of<int, Armor> 
        (1, Armor("Meito Ichimonji", 4, 150, 1)) 
        (2, Armor("Shusui", 10, 230, 2))
        // ....Rest of your map declarations
        (6, Armor("Soul Calibur", 60, 900, 8));

But if you don't want to add boost as a dependency to your program, your best bet would probably be to do something like this:

void ArcherArmor::ArcherArmor_shop(){
    soldier_armor[1] = Armor("Meito Ichimonji", 4, 150, 1);
    soldier_armor[2] = Armor("Shusui".......
    //and so on....

Edited by JasonHippy


I didn't mean that boost was bad. Far from it! The boost libraries are great!
It's a portable, mature, stable, well-tested set of C++ libraries that are extremely useful.

My comment refers to the fact that some people don't like making their programs rely on an external library for something trivial like this. If you compare the two examples in my post; the 2nd example is extremely simple and understandable and doesn't require a 3rd party library.

At the end of the day, boost::assign::map_list_of is little more than syntactic sugar. It provides an alternate means of initialising a std::list.

Some people (including myself) would consider it overkill to add the boost library as a dependency for just map_list_of! (The only reason I mentioned map_list_of was because it allows you to use similar syntax to your original code)

But the boost library is a great library. I heartily recommend taking a look at it. There are some extremely useful classes available in there.

If you want to use boost, by all means do so!
Personally, I'll only use the syntactic sugar provided by boost::assign if I'm already using other, more significant boost components elsewhere in my program (boost::bind, boost::signals2, boost::filesystem, boost::graph etc). In for a penny, in for a pound and all that! Heh heh! :)

Edited by JasonHippy


@ OP if you have a compiler that supports c++11 than you can use the std::map initializer list constructor. It would change your code to loo like this

void ArcherArmor::ArcherArmor_shop(){
    soldier_armor = {//name, damage, price
            std::make_pair(1, Armor("Meito Ichimonji", 4, 150, 1)),
            std::make_pair(2, Armor("Shusui", 10, 230, 2)),
            std::make_pair(3, Armor("Apocalypse", 16, 300, 3)),
            std::make_pair(4, Armor("Blade of Scars", 24, 550, 4)),
            std::make_pair(5, Armor("Ragnarok", 32, 610, 5)),
            std::make_pair(6, Armor("Eternal Darkness", 40, 690, 6)),
            std::make_pair(7, Armor("Masamune", 52, 750, 7)),
            std::make_pair(8, Armor("Soul Calibur", 60, 900, 8))

@jasonHippy, thank you so much for taking your time to explain everything clearly and give me clear tips and suggestion! :D. And, @NathanOliver i tried that, but it doesnt work. I guess I dont have c++11.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.