初始化地图时出错

时间:2014-02-09 22:34:48

标签: c++ class map initializer-list

好吧,在我的ArcherArmor.cpp中,我试图弄清楚为什么地图初始化列表不起作用,但由于某种原因我不断得到“错误C2593:'operator ='是模棱两可的”。这是我的代码:

我还有一个ArcherArmor派生自的类,它有一个名为Armor的结构,我为了简单起见而遗漏了这个结构。主要问题是错误!我唯一能想到的就是我错误地初始化地图。

//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*/

1 个答案:

答案 0 :(得分:0)

这里给出的代码部分似乎没问题。我使用元组类型来替换你的Armor类型(在你的问题中没有显示),并且代码用gcc(4.8.1)编译得很好。我认为问题出在代码的其他地方。

//ArcherArmor.h
#include <string>
#include <map>

#include <tuple>
using namespace std;
using Armor = std::tuple<string,int,int,int>;

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

int main() {
   ArcherArmor a;
   return 0;
}
相关问题