Hi
I am writing classes for a card game and trying to use vector for my deck. However when i run the program, it spites out seg fault.
The place where seg fault appears is in this function
Code:
void CardItem::Add(const vector<Card>& addon, unsigned int pos){
Require(pos < addon.size());
vector<Card>::iterator position = subdeck.begin()+pos;
cout << endl << "Add(const vector<Card>& addon, int pos) " << endl;
for(unsigned int i = 0; i < addon.size(); ++i){
cout << i << " val " << addon[i].getVal() << " suit " << addon[i].getSuit() << endl;
subdeck.push_back(addon[i]);
subdeck.insert(position+i, subdeck.back());
subdeck.pop_back();
}
}
before subdeck.push_back...., the cout right above the push_back is fine thru.
The class of CardItem looks something like this
Code:
class CardItem{
public:
CardItem(){};
virtual ~CardItem(){};
virtual void Shuffle(int flag);
virtual void Sort(int flag);
vector<Card>* Remove(const unsigned int start, const unsigned int end);
vector<Card>* Remove(const val_t val, const suit_t suit);
void Add(const vector<Card>& addon, unsigned int pos);
void Add(const val_t val, const suit_t suit);
int getNumCards() const {return subdeck.size();}
protected:
vector<Card> subdeck;
};
class Hand:public CardItem{
public:
Hand(){};
virtual ~Hand(){};
virtual void Sort(int flag);
private:
vector<Card> putDown;
};
class BaseDeck:public CardItem{
public:
BaseDeck(){};
virtual ~BaseDeck(){};
virtual int CreateBase(const val_t start, const val_t end, const suit_t suit_start, const suit_t suit_end);
private:
};
The function is called in the following code
Code:
void CardGame::Deal(unsigned int player){
vector<Card> *temp;
temp = baseDeck.Remove(1,1);
players[player].Add(*temp, 0);
delete temp;
}
I am not sure whether this is the right way to pass the content of a vector around, but it is the only way that I can think of.
I would really appreciate if someone can give me some idea on the seg fault
Cheers