2015-12-14 65 views
-2

我有幾個繼承類,但我不知道如何創建一個默認的構造函數,地圖其實是一張地圖,包括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); 
}; 

我只是想知道如何編寫該類的默認構造函數。

我的編譯器不斷顯示錯誤是:

./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(){} 
^ 
+0

什麼'std :: map >'又代表了什麼? – LogicStuff

+0

默認構造函數* what *?您已經定義了默認的構造函數... – Nim

+0

這個問題與異常無關。 – aschepler

回答

0

編譯器試圖告訴你什麼是錯在這裏:

沒有從已知的轉換 '常量的std ::地圖<的std :: string,性病::矢量<的std :: string>>'

所以,顯然facts是常量,但所有的(非常奇怪的)構造函數接受非const的引用,所以它們不能被使用。

+0

所以如何改變我極其奇怪的構造函數 –

+0

如果你想傳遞一個常量映射,參數應該是一個const引用。從整個容器創建單個對象是非常不可用的。這就是有點「奇怪」。 –

+0

糟糕 - 「不可用」應該是「不尋常的」。非常有幫助的拼寫更正! –

0

你的錯誤是很清楚的,收拾殘局,這是你到底是什麼了:從「const std::map<std::string, std::vector<std::string> >

沒有已知的轉換...

候選人的構造並不可行:第一個參數( 'const std::map<std::string, std::vector<std::string> >')將失去ç onst限定詞Charmander(std::map<std::string, std::vector<std::string> > &facts);

您正在將一個const對象傳遞給一個構造函數,該構造函數接受一個非const引用。檢查你的構造函數接受const正確性的事實。