cant figure out how...(struct + class)

This is a discussion on cant figure out how...(struct + class) within the C++ Programming forums, part of the General Programming Boards category; I feel like a complete idiot. Im trying to get a structure in my class which is a deck and ...

  1. #1
    *this
    Join Date
    Mar 2005
    Posts
    498

    cant figure out how...(struct + class)

    I feel like a complete idiot. Im trying to get a structure in my class which is a deck and I want a pointer to the deck which I can use throughout the class. Please point me in the right direction. Is my syntax way off?

    Code:
    #ifndef DECK_H_CLASS_01
    #define DECK_H_CLASS_01
    
    /*===================Card Structure===================*/
    struct card
    {
       int value;                      //card value
       int suit;                       //card suit
       bool dealt;                     //is it dealt?
       
       card (int v, int s, bool d);    //default constructor
    };
    
    card::card (int v, int s, bool d)
       : value(v), suit(s), dealt(d) { }
    /*====================================================*/
    
    const int deckSize = 52;     //standard deck size
    
    class deck
    {
       public:
          deck ();               //default constructor
          void shuffle ();       //shuffle the deck
          void deal (card);      //mark a card dealt
          ~deck ();              //default desctructor
       private:
          struct myDeck          //deck with 52 cards
          {
             card deckCards[deckSize];
          } *standardDeck;       //pointer to the deck
    };
    
    deck::deck ()
    {  
       for (int suit = 1; suit <= 4; suit++)
       {
          for (int a = 1; a <= 13; a++)
          {
             standardDeck->deckCards[a-1].value = a;
             standardDeck->deckCards[a-1].suit = suit;
             standardDeck->deckCards[a-1].dealt = false;
          }
       }
    }
    
    void deck::shuffle ()
    {
    }
    
    void deck::deal (card)
    {
    }
    
    deck::~deck ()
    {
    }
    
    #endif

  2. #2
    Registered User
    Join Date
    Aug 2003
    Posts
    127
    i think using standardDeck as yours is troublesome, std::vector should be used instead
    Nana C++ Library is a GUI framework that designed to be C++ style, cross-platform and easy-to-use.

  3. #3
    *this
    Join Date
    Mar 2005
    Posts
    498
    indexing the deckCards can be seen as faster, plus its static and will not change, so I have no use for a vector instead. my problem is that I cant figure out a way to create a pointer to my standard deck and allocate myDeck to its memory.

  4. #4
    Registered User manofsteel972's Avatar
    Join Date
    Mar 2004
    Posts
    317
    Code:
     
    struct card* pointer; //defines a pointer of type struct card
     
    you can also do it when you create the struct
     
    struct card
    {
    int data;
    int data1;
    } *pointer;
    Where do you create your deck object? All I see is your struct declaration and the pointer?
    Last edited by manofsteel972; 05-18-2005 at 05:34 PM.
    "Knowledge is proud that she knows so much; Wisdom is humble that she knows no more."
    -- Cowper

    Operating Systems=Slackware Linux 9.1,Windows 98/Xp
    Compilers=gcc 3.2.3, Visual C++ 6.0, DevC++(Mingw)

    You may teach a person from now until doom's day, but that person will only know what he learns himself.

    Now I know what doesn't work.

    A problem is understood by solving it, not by pondering it.

    For a bit of humor check out xkcd web comic http://xkcd.com/235/

  5. #5
    *this
    Join Date
    Mar 2005
    Posts
    498
    sorry I dont quite understand what your saying, isnt that what I've already done for the deck, why would I want it for the cards. Also when you declare a pointer like that, does it allocate memory for you? because my current code gives me a runtime error, but the syntax is fine with the compiler, I dont know if I'm doing the right thing for what I want. What I mean to say is I am confused with the syntax in that I dont know if memory is already allocated to the pointer if it isnt how would I go about doing that to work with my code.

  6. #6
    Registered User manofsteel972's Avatar
    Join Date
    Mar 2004
    Posts
    317
    ok my mistake but the form is still the same but all you have done for myDeck is declare the struct you still need to create an object to work on? myDeck someDeck; and then assign the pointer to the deck standardDeck=&someDeck;
    "Knowledge is proud that she knows so much; Wisdom is humble that she knows no more."
    -- Cowper

    Operating Systems=Slackware Linux 9.1,Windows 98/Xp
    Compilers=gcc 3.2.3, Visual C++ 6.0, DevC++(Mingw)

    You may teach a person from now until doom's day, but that person will only know what he learns himself.

    Now I know what doesn't work.

    A problem is understood by solving it, not by pondering it.

    For a bit of humor check out xkcd web comic http://xkcd.com/235/

  7. #7
    *this
    Join Date
    Mar 2005
    Posts
    498
    I get what your saying, and tried that but I got some errors:

    C:/Documents and Settings/JR/My Documents/C ++/Blackjack/deck.h: In
    constructor `deck::deck()':
    C:/Documents and Settings/JR/My Documents/C ++/Blackjack/deck.h:36: error: no
    matching function for call to `deck::myDeck::myDeck()'
    C:/Documents and Settings/JR/My Documents/C ++/Blackjack/deck.h:29: error: candidates
    are: deck::myDeck::myDeck(const deck::myDeck&)
    I also tried this, thought it would be better, but got the same errors:
    Code:
    #ifndef DECK_H_CLASS_01
    #define DECK_H_CLASS_01
    
    /*===================Card Structure===================*/
    struct card
    {
       int value;                      //card value
       int suit;                       //card suit
       bool dealt;                     //is it dealt?
       
       card (int v, int s, bool d);    //default constructor
    };
    
    card::card (int v, int s, bool d)
       : value(v), suit(s), dealt(d) { }
    /*====================================================*/
    
    const int deckSize = 52;     //standard deck size
    
    class deck
    {
       public:
          deck ();               //default constructor
          void shuffle ();       //shuffle the deck
          void deal (card);      //mark a card dealt
          ~deck ();              //default desctructor
       private:
          struct myDeck          //deck with 52 cards
          {
             card deckCards[deckSize];
          } *deckPtr;            //pointer to the deck
    };
    
    deck::deck ()
    {  
       deckPtr = new myDeck;
       for (int suit = 1; suit <= 4; suit++)
       {
          for (int a = 1; a <= 13; a++)
          {
             deckPtr->deckCards[a-1].value = a;
             //standardDeck->deckCards[a-1].suit = suit;
             //standardDeck->deckCards[a-1].dealt = false;
          }
       }
    }
    
    void deck::shuffle ()
    {
    }
    
    void deck::deal (card)
    {
    }
    
    deck::~deck ()
    {
    }
    
    #endif

  8. #8
    Registered User manofsteel972's Avatar
    Join Date
    Mar 2004
    Posts
    317
    Code:
    struct card
    {
        int c;
        int b;
    };
    
    const int n=52;
    class deck
    {
    public:
        
        deck();
    private:
        card newcard[n];
        card*pointer;
    };
    
    
    deck::deck()
    {
        pointer=newcard;
    }
    here is an example of what i mean
    "Knowledge is proud that she knows so much; Wisdom is humble that she knows no more."
    -- Cowper

    Operating Systems=Slackware Linux 9.1,Windows 98/Xp
    Compilers=gcc 3.2.3, Visual C++ 6.0, DevC++(Mingw)

    You may teach a person from now until doom's day, but that person will only know what he learns himself.

    Now I know what doesn't work.

    A problem is understood by solving it, not by pondering it.

    For a bit of humor check out xkcd web comic http://xkcd.com/235/

  9. #9
    *this
    Join Date
    Mar 2005
    Posts
    498
    thanks a bunch, hadnt realized I could do it that way, so much easier

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. returning class and struct members
    By simone.marras in forum C++ Programming
    Replies: 17
    Last Post: 03-16-2009, 11:10 AM
  2. Looking for a way to store listbox data
    By Welder in forum C Programming
    Replies: 20
    Last Post: 11-01-2007, 11:48 PM
  3. Mmk, I give up, lets try your way. (Resource Management)
    By Shamino in forum Game Programming
    Replies: 31
    Last Post: 01-18-2006, 08:54 AM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 09:33 AM
  5. gcc problem
    By bjdea1 in forum Linux Programming
    Replies: 13
    Last Post: 04-29-2002, 06:51 PM

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