Thread: problem using pointers

  1. #1
    Registered User
    Join Date
    Dec 2005
    Posts
    118

    problem using pointers

    hi there i seem to have a problem setting my array of cards to null, well i think thats the problem
    Code:
    class deck {
    public:
           deck();      //Constructor
      
      void createDeck();//Fills array with legal cards
      void shuffle();   //Shuffles those cards
      card drawCard();  //Gets a card from the deck
      card* cards[52];
    };
    
    deck::deck() {
      
      createDeck();        //Set up the deck
    }
    how would i set the array of cards to null?
    maybe if i tell that when i run my program it crashes straight away but it does compile i can get more info if you need it but i wouldnt know what you need
    thanks in advance
    Code:
    class card
    {
    public:
    	int value;
    	char suit;
    };
    this is my card class

    my problem lies in that im trying to test my code by "cout<<" the deck out into a consol but it doesnt work it just crashes
    Last edited by thestien; 05-02-2008 at 03:54 PM.

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    > card* cards[52];
    This most likely should be:
    card cards[52];

    You could also use a vector:
    Code:
    vector<card> cards;
    Then adding a card is easy:
    Code:
    card Card = {10, 'd'};
    cards.push_back(Card);

  3. #3
    Registered User
    Join Date
    Dec 2005
    Posts
    118
    ah ha super that now displays a load of junk in the consol but it dont crash so its a start for me to work on

    thanks for the advice

    im gonna try to use a vector for the hands in my game as ive never used one before
    so hopefully this project is gonna be fun
    Last edited by thestien; 05-02-2008 at 04:13 PM.

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Your card class could have a constructor that sets the value and suit to some value meaning "unspecified". That way you won't get junk.

    Of course, your deck constructor should probably initialize the cards to valid values anyway and that would fix the problem as well. You have a createDeck function but good design normally has the object initialized properly in the constructor (in this case that means creating the deck).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with file handling (pointers)
    By hmk in forum C Programming
    Replies: 5
    Last Post: 09-19-2008, 10:03 AM
  2. Problem with pointers
    By kotoko in forum C Programming
    Replies: 3
    Last Post: 06-12-2008, 05:17 AM
  3. A problem with pointers
    By vsla in forum C Programming
    Replies: 2
    Last Post: 10-10-2007, 04:14 AM
  4. Returning pointer to array of pointers problem
    By jimzy in forum C Programming
    Replies: 15
    Last Post: 11-11-2006, 06:38 AM
  5. Problem writing swap using pointers
    By joshdick in forum C++ Programming
    Replies: 1
    Last Post: 02-29-2004, 10:06 PM