继承与异常

Inheritance and exceptions

我有几个继承 class,但我不知道如何为此创建一个默认构造函数,地图事实是一个包含口袋妖怪 ID 名称和其他内容的地图

class Pokemon {
public:
     Pokemon(){}
     Pokemon(std::map<std::string, std::vector<std::string> > &facts);
protected:
     std::map<std::string, std::vector<std::string> > facts_;
};

class Dragon: virtual public Pokemon {
public:
    Dragon() : Pokemon() {}
    Dragon(std::map<std::string, std::vector<std::string> > &facts);
};

class Monster: virtual public Pokemon {
public:
    Monster() : Pokemon() {}
    Monster(std::map<std::string, std::vector<std::string> > &facts);
};   

class Charmander: public Monster, public Dragon {
public:
    Charmander() : Pokemon(), Monster(), Dragon(){}
    Charmander(std::map<std::string, std::vector<std::string> > &facts);
};

class Charmeleon: public Charmander {
public:
    Charmeleon() : Charmander() {}
    Charmeleon(std::map<std::string, std::vector<std::string> > &facts);
};

class Charizard: public Charmeleon {
public:
    Charizard() : Charmeleon() {}
    Charizard(std::map<std::string, std::vector<std::string> > &facts);
};

我只想知道如何编写 class 的默认构造函数。

我的编译器不断显示错误:

./List07.txt:10:10: error: no matching constructor for initialization of
  'Charmander'
POKENAME(Charmander)
~~~~~~~~~^~~~~~~~~~~
main.cpp:276:44: note: expanded from macro 'POKENAME'
#define POKENAME(type)  try { answer = new type(facts); } catch (int) {
                                       ^
./pokemon.h:148:7: note: candidate constructor (the implicit copy constructor)
      not viable: no known conversion from 'const std::map<std::string,
      std::vector<std::string> >' to 'const Charmander' for 1st argument
class Charmander: public Monster, public Dragon {
  ^
./pokemon.h:151:5: note: candidate constructor not viable: 1st argument ('const
  std::map<std::string, std::vector<std::string> >') would lose const
  qualifier
Charmander(std::map<std::string, std::vector<std::string> > &facts);
^
./pokemon.h:150:5: note: candidate constructor not viable: requires 0 arguments,
  but 1 was provided
Charmander() : Pokemon(), Monster(), Dragon(){}
^

你的错误很明显,清理混乱,这就是你最终得到的结果:

没有来自 'const std::map<std::string, std::vector<std::string> >' ...

的已知转换

候选构造函数不可行:第一个参数('const std::map<std::string, std::vector<std::string> >')将丢失 const 限定符 Charmander(std::map<std::string, std::vector<std::string> > &facts);

您正在将 const 对象传递给采用非常量引用的构造函数。检查接受常量正确性事实的构造函数。

编译器试图告诉你这里出了什么问题:

no known conversion from 'const std::map<std::string, std::vector<std::string> >'

所以,显然 facts 是 const,但是你所有的(非常奇怪的)构造函数都引用了非常量,所以它们不能被使用。