Simple class embedding question

Discussion in 'C++' started by hexusnexus@gmail.com, Apr 9, 2008.

  1. Guest

    Hi. I just downloaded Visual C++ Express, and I converted my C code
    to C++. The problem I'm having is that I'm declaring a new class
    instance inside another class definition, and I can't get access to
    the data. Actually, I'm using structs, but they're only default public
    classes anyway, so I know at least that it can't be a problem with
    protected or private.

    Here's a snippet of the code:


    /****germanwhist.h*****/

    struct Hand
    {
    Card cards[13];
    int length;
    Hand()
    {
    length=0;
    }
    bool operator! ()
    {
    int i;
    bool j;
    for (i=0; i < Len(); i++)
    {
    if (cards.raw==-1)
    {
    j=true;
    break;
    }
    if (j==true) return true;
    else return false;
    }
    }
    Card pull(int pos=0)
    {
    Card temp;
    temp = cards[0];
    cards[0] = Card();
    length--;
    return temp;
    }
    Card pullName(string name)
    {
    Card pulled;
    for (int i=0; i < Len(); i++)
    {
    if (cards.name==name)
    {
    pulled=pull(i);
    break;
    }
    }
    return pulled;
    }
    void put(Card crd)
    {
    cards[length] = crd;
    length++;
    }
    Card* inSuit(Card trump)
    {
    Card temp[13];
    for (int i=0; i < Len(); i++)
    if (cards.s==trump.s) temp=cards;
    return temp;

    }
    Card* notinSuit(Card trump)
    {
    Card temp[13];
    for (int i=0; i < Len(); i++)
    if (cards.s!=trump.s)
    temp=cards;
    return temp;
    }
    Card* allSuits()
    {
    return cards;
    }
    void organize(Card trump)
    {
    Card temp;
    int i, j, k, l, m, begin, end;
    for (i=0; i<Len(); i++)
    {
    for (j=0; j<Len()-1; j++)
    {
    if (cards[j].raw > cards[j+1].raw)
    {
    temp = cards[j+1];
    cards[j+1] = cards[j];
    cards[j] = temp;
    }
    }
    }

    //move trumps to top
    for (i=Len()-1; i>-1; i--)
    if (cards.s == trump.s) break;

    end = i;
    if (end<0) end=0;

    for (i=end; i>-1; i--)
    if (cards.s != trump.s) break;

    begin = i;

    m=Len()-1;
    for (j=end; j > begin; j--)
    {
    l=0;
    k=j;
    while (true)
    {
    l=k+1;
    if (l > m) break;
    temp = cards[l];
    cards[l] = cards[k];
    cards[k] = temp;
    k=l;
    }
    m--;
    }
    //move negative (null) cards to the top to be ignored
    if (Len()<13)
    {
    for (i=0; i<Len(); i++)
    {
    for (j=0; j<Len()-1; j++)
    {
    if (!cards[j])
    {
    temp = cards[j+1];
    cards[j+1] = cards[j];
    cards[j] = temp;
    }
    }
    }
    }
    }
    int Len()
    {
    return length;
    }
    };

    struct Player
    {
    Hand hand;
    string name;
    int tricks;
    Player(string nname)
    {
    name = nname;
    tricks = 0;
    }
    void receiveCard(Card crd)
    {
    hand.put(crd);
    }
    void wonTrick()
    {
    tricks++;
    }
    void organize(Card trump)
    {
    hand.organize(trump);
    }
    int Len()
    {
    return hand.length;
    }
    };

    ****germanwhist.cpp****
    int main()
    {
    Player player("Player");
    ... //add cards to player hand
    cout << .player.Len() << endl;
    system("pause");
    return 0;
    }

    //player.Len() should return the number of cards in the player hand.
    But it always returns 0. I've searched
    //throughout the code and I can't seem to find any answers. Anybody
    have any ideas?
     
    , Apr 9, 2008
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. E11
    Replies:
    1
    Views:
    4,895
    Thomas Weidenfeller
    Oct 12, 2005
  2. christopher diggins
    Replies:
    16
    Views:
    774
    Pete Becker
    May 4, 2005
  3. Themistoklis Bourdenas

    Failing in a simple embedding example of python

    Themistoklis Bourdenas, Mar 1, 2006, in forum: Python
    Replies:
    0
    Views:
    301
    Themistoklis Bourdenas
    Mar 1, 2006
  4. Christian Maier
    Replies:
    3
    Views:
    506
    John Harrison
    Feb 15, 2007
  5. Replies:
    7
    Views:
    1,996
    Farshid Lashkari
    Oct 31, 2007
Loading...

Share This Page