seg fault

This is a discussion on seg fault within the C++ Programming forums, part of the General Programming Boards category; Hi I am writing classes for a card game and trying to use vector for my deck. However when i ...

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    3

    seg fault

    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

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,892
    My guess is that the push_back invalidates the position iterator. It also looks like you're inserting each card twice.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting a seg fault
    By ammochck21 in forum C Programming
    Replies: 11
    Last Post: 01-23-2009, 04:27 AM
  2. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  3. seg fault at vectornew
    By tytelizgal in forum C Programming
    Replies: 2
    Last Post: 10-25-2008, 01:22 PM
  4. weird seg fault
    By Vermelho in forum C Programming
    Replies: 3
    Last Post: 05-10-2008, 08:27 PM
  5. Seg Fault Problem
    By ChazWest in forum C++ Programming
    Replies: 2
    Last Post: 04-18-2002, 03:24 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21