Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ problems with array of objects

Tags:

c++

arrays

object

We have an assignment to create a game of blackjack.

Bellow is simplified version of my code:

#include <iostream>
#include <string>
#include <time.h>

using namespace std;

class Deck
{
private:
    Card cards[52];    <-- HERE!!
public:

};

class Card
{
private:
    int suit;
    int number;
public:


    int getSuit();
    int getNumber();
    void setCard(int suit, int number);

};
int Card::getSuit()
{
    return suit;
}

int Card::getNumber()
{
    return number;
}

void Card::setCard(int s, int n)
{
    suit = s;
    number = n;
}

class Players
{
private:
    Card PlayersCards[10];
public: 
    /*Card getCard();*/


};

//Card Players::getCard()
//{
//  return;
//}

int main()
{
    Players user;


    cin.get();
    cin.get();
    return 0;
}

The problem is where the array of objects Card is being created. the compiler gives me the following errors:

Error C3646 'cards': unknown override specifier

Error C2143 syntax error: missing ',' before '['

Error C2143 syntax error: missing ')' before ';'

Error C2238 unexpected token(s) preceding ';'

What is wrong with my code?

like image 559
user5714811 Avatar asked Dec 24 '15 15:12

user5714811


1 Answers

The compiler doesn't know what Card is, so cannot generate the right code.

The class Card needs to be declared before the class Deck, as Card is included in the Deck.

class Card {
    /// stuff - allows compiler to work out the size of one Card.
};

class Deck {
  private:
    Card cards[52];    // knows how to create 52 of these.
};
// implementation can go later.
int Card::getSuit()
{
    return suit;
}
like image 192
mksteve Avatar answered Oct 08 '22 18:10

mksteve