Simple class embedding question
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[i].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[i].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[i].s==trump.s) temp[i]=cards[i];
return temp;
}
Card* notinSuit(Card trump)
{
Card temp[13];
for (int i=0; i < Len(); i++)
if (cards[i].s!=trump.s)
temp[i]=cards[i];
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[i].s == trump.s) break;
end = i;
if (end<0) end=0;
for (i=end; i>-1; i--)
if (cards[i].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?