7

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?

2
  • 2
    Because the compiler doesn't understand the class Card, it throws the parser, making the error messages quite obtuse. Commented Dec 24, 2015 at 15:14
  • I feel for you. The fact that C++ requires forward declarations at all (unlike other languages, cf C#, that do the work for you) plus the fact that the error message is misleading ... sigh. Why do we program in C++ again? :) Commented Dec 24, 2015 at 18:56

1 Answer 1

9

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;
}
Sign up to request clarification or add additional context in comments.

2 Comments

oh what a stupid mistake -.- no wonder I couldnt think of anything wrong with the code cause it was because of ordering ! anyway thanks for the help :)
@user5714811 Please mark the answer as accepted and upvote it if it has helped you to solve your problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.