0

I have a player class. In the class I have a hand of size three, which is of type card. Card is another class. I am trying to access the private data members of hand through class, but inside the player class. This is the error I get:

scat.cpp: In member function ‘void player::setHand(card*)’:
scat.cpp:117:10: error: request for member ‘cardCopy’ in     
‘((player*)this)->player::hand’, which is of pointer 
type ‘card*’ (maybe you meant to use ‘->’ ?)hand.cardCopy(c);

#include <iostream>
using namespace std;

class card{
    char *suit;
    char *rank;
    int cvalue;
    char *location;

public:
    card::cardCopy(card *c);
};
class player{

card *hand;
public:
    player::setHand(card *c);
};
void card::cardCopy(card *c)
{
  strcopy(rank, (*c).rank);
  strcopy(suit, (*c).suit);
  strcopy(location, (*c).location);
  cvalue = (*c).cvalue;
}
player::player()
   {
    name = new char[20];
    hand = new card[3];
}

void player::setHand(card*c)
  {
    hand.cardCopy(c);
  }

I dont understand why i cant access the function cardCopy like this..hand is a card type!!!

R Sahu
  • 204,454
  • 14
  • 159
  • 270
matt
  • 11
  • 4
  • 1
    How exactly does `hand*;` declare a "card type!!!" ? *Post **real** code.*. `card::cardCopy(card *c);` contains an extra qualification, as does `player::setHand`, `strcopy` has absolutely no declaration whatsoever, and `player::player()` isn't even in the class declaration, yet it has implementation. **Post the *real* code**. If this *is* your *actual* code, then honestly, *stop coding* and get thee to a [decent book on C++](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) – WhozCraig Oct 15 '14 at 04:53
  • card *hand; on about line 10.. @WhozCraig – matt Oct 15 '14 at 04:56

2 Answers2

4

since your "hand" is a pointer you have to use -> operator to access member variables and functions

class player{
 card *hand // member pointer of card type
}

void player::setHand(card *c)
{
  hand->cardCopy(c);
}
Kevin
  • 56
  • 6
  • 3
    If the text in the error message: `maybe you meant to use ‘->’ ?` didn't seal the deal, maybe this answer *will*. – WhozCraig Oct 15 '14 at 05:00
2

Well, there are several problems with your code that i can see:

  • In you class declaration, you are declaring functions without specifying a return type.

  • *hand does not have a type in its declaration.

  • hand is a pointer, so you need to use the -> operator to access its members.

  • hand is being initialized to an array of length 3 in the constructor, yet you are not specifying which element you are trying to access.

Try this:

#include <iostream>
#define strcopy(a,b) *a=*b
using namespace std;

class card
{
private:
    char *suit;
    char *rank;
    int cvalue;
    char *location;
public:
    void cardCopy(card *c);
};

class player
{
private:
    char *name;
    card *hand;
public:
    player();
    void setHand(card *c);
};

void card::cardCopy(card *c)
{
  strcopy(rank, c->rank);
  strcopy(suit, c->suit);
  strcopy(location, c->location);
  cvalue = c->cvalue;
}

player::player()
{
    name = new char[20];
    hand = new card[3];
}

void player::setHand(card*c)
{
    hand[0].cardCopy(c);
}

Of course, you will also need to provide an implementation of strcopy() within this scope in order for this to work. I have provided one for convenience, but i can not be sure that my implementation will do exactly what you want it to do.

android927
  • 303
  • 1
  • 12
  • how would i access other data members other than 0? as in how would i cycle through the hand and assign card values to it? @android927, i also have everything else correct, i retyped my code and left out somethings by mistake – matt Oct 15 '14 at 06:13
  • Use a for loop that goes from 0 to 1 less than the number of cards in the hand. In this case: `for(int i=0;i<3;i++)` – android927 Oct 15 '14 at 07:12
  • 1
    Also, might i ask why you are using a pointer to a char[] for the name instead of an std::string? – android927 Oct 15 '14 at 07:16
  • its a project for a class and we are required to use c-style strings...hence having to write my own strcopy and such – matt Oct 15 '14 at 20:22