2014-05-15 79 views
0

我一直在嘗試使用集合作爲類的屬性。這些集合需要在類初始化時進行初始化。我一直在嘗試使用預初始化的數組,然後將這些集的值設置爲數組的內容。然而,我的問題是,它似乎只能將set的內容設置爲聲明集時數組的內容,而如果它是class屬性,則不能這樣做。我寧願不單獨添加每件作品。任何建議?下面是一些示例代碼: Creature.cpp:使用集合作爲與類別初始化的屬性

 Creature :: Creature() 
{ 
    Skill Init[] = {Balance,EscapeArtist,Hide,MoveSilently,OpenLock,Ride,SleightOfHand,Tumble,UseRope}; 
    //DexSkills = ??? (contents of init) 
    Skill Init[] = {Climb,Jump,Swim}; 
    //StrSkills = ??? 
    Skill Init[] = {Concentration}; 
    //ConSkills = ??? 
    Skill Init[] = {Appraise,Craft,DecipherScript,DisableDevice,Forgery,Knowledge,Psicraft,Search,Spellcraft}; 
    //IntSkills = ??? 
    Skill Init[] = {Autohypnosis,ControlShape,Heal,Listen,Profession,SenseMotive,Spot,Survival}; 
    //WisSkills = ??? 
    Skill Init[] = {Bluff,Diplomacy,Disguise,GatherInformation,HandleAnimal,Intimidate,Perform,UseMagicDevice,UsePsionicDevice}; 
    //ChaSkills = ??? 
} 

和Creature.h:

#pragma once 
#define roll20 (rand()%20) + 1 
#define Mod(stat) ((stat-10)/2) 
#include <vector> 
#include <set> 
#include "Global.h" 


class Creature 
{ 
protected: 
std::set<Skill> DexSkills; 
std::set<Skill> StrSkills; 
std::set<Skill> ConSkills; 
std::set<Skill> IntSkills; 
std::set<Skill> WisSkills; 
std::set<Skill> ChaSkills; 
//static const Skill StrSkills[3]; 
//static const Skill ConSkills[1]; 
//static const Skill IntSkills[9]; 
//static const Skill WisSkills[8]; 
//static const Skill ChaSkills[9]; 
const int maxHP; 
int HP; 
int hitDie; 
int speed; 
int babBase; 
int fortBase; 
int refBase; 
int willBase; 
int AC; 
int STR; 
int CON; 
int DEX; 
int INT; 
int WIS; 
int CHA; 
DamageResist DR; 
std::vector<Feat> Feats; 
Alignment alignment; 
//std::set<DamageType> Weaknesses; 
//std::set<DamageType> Resistances; 
std::set<DamageType> Immunities; 
int Resistances [15]; 
int SkillRanks[40]; 

public: 
//Creature(); 
int Damage(int damage, DamageType type); 
void getDamaged(int damage); 
int StatRoll(int stat); 
//int StrRoll(); 
//int ConRoll(); 
//int DexRoll(); 
//int IntRoll(); 
//int WisRoll(); 
//int ChaRoll(); 
int SkillCheck(Skill skill); 

};

,並在我的global.h:

typedef enum { 
Appraise,Autohypnosis,Balance,Bluff,Climb,Concentration,ControlShape,Craft,DecipherScript,Diplomacy,DisableDevice,Disguise,EscapeArtist,Forgery, 
GatherInformation,HandleAnimal,Heal,Hide,Intimidate,Jump,Knowledge,Listen,MoveSilently,OpenLock,Perform,Profession,Psicraft,Ride,Search,SenseMotive, 
SleightOfHand,SpeakLanguage,Spellcraft,Spot,Survival,Swim,Tumble,UseMagicDevice,UsePsionicDevice,UseRope 
} Skill; 
+0

是什麼你看到的問題? – tinkertime

+0

你的問題有點不清楚。你只是想在構造函數中初始化一些集合嗎?或者這些集合是否是靜態類成員呢? – Rook

回答

0

如果你正在尋找一種簡單的方法來C數組複製到一組,你可以嘗試這樣的事:

#include <iterator> 

... 

int a[] = { 1, 2, 3, 4, 5, 6 }; 
int n = sizeof(a)/sizeof(a[0]); 

std::set<int> s; 
std::copy(a, a + n, std::inserter(s, s.end()));