Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error: Member is inaccessible

I have these two classes:

class Hand
{
public:
    int getTotal();
    std::vector<Card>& getCards();
    void add(Card& card);
    void clear();
private:
    std::vector<Card> cards;
};

class Deck : public Hand
{
public:
    void rePopulate();
    void shuffle();
    void deal(Hand& hand);
};

Where the shuffle() function is declared as follows:

void Deck::shuffle()
{
    std::random_shuffle(cards.begin(), cards.end());
}

However, this returns the following error:

'Hand::cards' : cannot access private member declared in class 'Hand'

Should I just include a function such asstd::vector<Card>& getCards() or is there another way to avoid the error.

like image 235
LazySloth13 Avatar asked Jul 27 '13 08:07

LazySloth13


1 Answers

You can declare cards as protected:

class Hand
{
public:
    int getTotal();
    std::vector<Card>& getCards();
    void add(Card& card);
    void clear();
protected:
    std::vector<Card> cards;
};

class Deck : public Hand
{
public:
    void rePopulate();
    void shuffle();
    void deal(Hand& hand);
};
like image 73
Ahmed Masud Avatar answered Sep 30 '22 07:09

Ahmed Masud