2

I get these errors each time I try to declare an instance of another object Hand as a parameter of a method of another class on the same header and source files.

Even though the declaration and implementation statements are the same.

Errors:


Severity    Code    Description Project File    Line    Suppression State
Error (active)  E0020   identifier "Hand" is undefined  Risk    
C:\Users\ebrah\source\repos\Risk\Risk\Risk\Cards.h  44  
Severity    Code    Description Project File    Line    Suppression State Error (active)    
E0147   declaration is incompatible with "void Deck::draw(<error-type> &hand)" 
(declared at line 44 of "C:\Users\ebrah\source\repos\Risk\Risk\Risk\Cards.h")   
Risk C:\Users\ebrah\source\repos\Risk\Risk\Risk\Cards.cpp   186 

My code:


In the header file:

class Card {
private:
    static unsigned int nextCardID;
    unsigned int cardID;
    CardType cardType;

public:
    Card();
    ~Card();
    //Card(const Card &card);
    Card(CardType armyType);
    CardType getArmyType() const;
    void setArmyType(CardType &armytype);
    int getCardID() const;
    void play(list<Card*> handCards, CardType& card);
    friend ostream& operator<<(ostream& strm, const Card& card);
};
class Deck{
    private:
    list<Card> allCards;
    list<Card*> deckCards;
    void generateCard(int numberOfCounteries);
public:
    Deck();
    ~Deck();
    Deck(int numberOfCounteries);
    void draw(Hand& hand);
    friend ostream& operator<<(ostream& strm, const Deck& deck);
    list<Card> getAllCards() const;
    void setAllCards(list<Card> allCards);
    list<Card*> getDeckCards() const;
    void setDeckCards(list<Card*> deckCards);
};
class Hand {
private:
    list<Card*> handCards;
public:
    Hand();
    ~Hand();
    friend ostream& operator<<(ostream& strm, const Hand hand);
    list<Card*> getHandCards() const;
    void setHandCards(list<Card*> handCards);
};

In the source file:

void Deck::draw(Hand& hand)
{

}

I also would like to know how to return a reference to a list.

Thank you.


anastaciu
  • 23,467
  • 7
  • 28
  • 53
Ebraheem
  • 23
  • 5
  • Move the declaration of `class Hand` above the declaration of `class Deck`. – HolyHoratio Oct 06 '20 at 08:22
  • Thank you, but Hand has a member of type Card and I get the same error if I write the implementations for getters and setters of that member. – Ebraheem Oct 06 '20 at 08:36

1 Answers1

0

You can forward declare Hand before the definition of the Deck class:

class Hand; //<-- here

class Deck{
    private:
    list<Card> allCards;
    list<Card*> deckCards;
    void generateCard(int numberOfCounteries);
public:
    Deck();
    ~Deck();
    Deck(int numberOfCounteries);
    void draw(Hand& hand);
    friend ostream& operator<<(ostream& strm, const Deck& deck);
    list<Card> getAllCards() const;
    void setAllCards(list<Card> allCards);
    list<Card*> getDeckCards() const;
    void setDeckCards(list<Card*> deckCards);
};

//...

To return a reference to the list just add & to the method, i.e.:

list<Card>& getAllCards() const;

Note that this is a const reference, you cannot change the list in the caller, this is good practice if you want to access the data and not change it, you avoid the overhead of returning by copy.

If you wish to change the list you would need to remove the const keyword.

anastaciu
  • 23,467
  • 7
  • 28
  • 53