C++ 继承和例外
我有几个继承类,但我不知道如何为它创建默认构造函数,映射事实上是一个包含ID名称和pokemon的其他内容的映射C++ 继承和例外,c++,inheritance,data-structures,default-constructor,C++,Inheritance,Data Structures,Default Constructor,我有几个继承类,但我不知道如何为它创建默认构造函数,映射事实上是一个包含ID名称和pokemon的其他内容的映射 class Pokemon { public: Pokemon(){} Pokemon(std::map<std::string, std::vector<std::string> > &facts); protected: std::map<std::string, std::vector<std::stri
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口袋妖怪{
公众:
口袋妖怪(){}
口袋妖怪(标准::地图和事实);
受保护的:
映射事实;
};
龙类:虚拟公共口袋妖怪{
公众:
龙():口袋妖怪(){}
龙(std::地图和事实);
};
类怪物:虚拟公共口袋妖怪{
公众:
怪物():口袋妖怪(){}
怪物(标准::地图和事实);
};
职业魔法师:公共怪物,公共龙{
公众:
魔法师():口袋妖怪(),怪物(),龙(){}
查曼德(标准::地图和事实);
};
类Charmeleon:公共Charmander{
公众:
Charmeleon():Charmander(){}
Charmeleon(标准::地图和事实);
};
Charizard类:公共Charmeleon{
公众:
Charizard():Charmeleon(){}
查里扎德(标准::地图和事实);
};
我只是想知道如何为类编写默认构造函数
我的编译器不断显示以下错误:
./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(){}
^
/List07.txt:10:10:错误:没有用于初始化的匹配构造函数
“魔术师”
波克南(查曼德)
~~~~~~~~~^~~~~~~~~~~
main.cpp:276:44:注意:从宏“POKENAME”展开
#定义POKENAME(类型)try{answer=新类型(事实);}catch(int){
^
./pokemon.h:148:7:注意:候选构造函数(隐式副本构造函数)
不可行:没有已知的第一个参数从“const std::map”到“const Charmander”的转换
职业魔法师:公共怪物,公共龙{
^
./pokemon.h:151:5:注意:候选构造函数不可行:第一个参数('const
std::map')将丢失常量
合格者
查曼德(标准::地图和事实);
^
./pokemon.h:150:5:注意:候选构造函数不可行:需要0个参数,
但提供了1个
魔法师():口袋妖怪(),怪物(),龙(){}
^
您的错误非常清楚,请清理这些乱七八糟的东西,这就是您的结局:
从“const std::map
”没有已知的转换
候选构造函数不可行:第一个参数(“const std::map
”)将丢失常量限定符Charmander(std::map&facts);
您正在向接受非常量引用的构造函数传递常量对象。请检查接受常量正确性事实的构造函数。编译器试图告诉您此处的错误: “const std::map”的转换未知
显然,事实是常数,但你所有的(非常奇怪)构造函数引用非常量,因此不能使用。std::map再次代表什么?默认构造函数代表什么?您已经定义了默认构造函数…这个问题与异常无关。因此,如何更改我非常奇怪的构造函数如果要传递常量映射,参数r应该是常量引用。从整个容器中创建单个对象是非常不可用的。这就是有点“奇怪”的地方。哦,“不可用”应该是“不寻常”。非常有用的拼写更正!