C++ problems with array of objects

时间:2022-07-21 21:17:23

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

错误C3646'卡':未知的覆盖说明符

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

错误C2143语法错误:在'['之前缺少','

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

错误C2143语法错误:在';'之前缺少')'

Error C2238 unexpected token(s) preceding ';'

错误C2238';'之前的意外令牌

What is wrong with my code?

我的代码出了什么问题?

1 个解决方案

#1


8  

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

编译器不知道Card是什么,因此无法生成正确的代码。

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

类卡需要在类Deck之前声明,因为卡包含在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;
}

#1


8  

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

编译器不知道Card是什么,因此无法生成正确的代码。

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

类卡需要在类Deck之前声明,因为卡包含在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;
}