1. ホーム
  2. c++

[解決済み] 文字列の配列の初期化文字列が長すぎるエラー

2022-02-24 21:19:27

質問

C++を使用してBlackJack Gameを作成していますが、その中に以下のようなコードがあり、エラーが発生します。

typedef struct
{
    int value;
    char suit[8];
    char name[8];
}Deck;


Deck Cards[52] = {{ 1,"Ace","Hearts"},{ 2, "Two","Hearts"}, { 3, "Three", "Hearts"}, { 4, "Four","Hearts"}, { 5,"Five","Hearts"},{ 6,"Six", "Hearts06"},
{ 7,"Seven","Hearts"},{ 8,"Eight","Hearts"},{ 9,"Nine","Hearts"},{ 10,"Ten","Hearts"},{10,"Jack","Hearts"},{10,"Queen","Hearts"},{10,"King","Hearts"},
{ 1,"Ace","Clubs"},{2, "Two", "Clubs"},{3,"Three","Clubs"},{4,"Four","Clubs"},{5,"Five","Clubs"},{6,"Six","Clubs"},{7,"Seven","Clubs"},{8,"Eight","Clubs"},
{ 9,"Nine","Clubs"},{10,"Ten","Clubs"},{10,"Jack","Clubs"},{10,"Queen","Clubs"},{10,"King","Clubs"},
{ 1,"Ace","Diamonds"},{2,"Two","Diamonds"},{3,"Three","Diamonds"},{4,"Four","Diamonds"},{5,"Five","Diamonds"},{6,"Six","Diamonds"},{7,"Seven","Diamonds"},
{ 8,"Eight","Diamonds"},{9,"Nine","Diamonds"},{10,"Ten","Diamonds"},{10,"Jack","Diamonds"},{10,"Queen","Diamonds"},{10,"King","Diamonds"},
{ 1,"Ace","Spades"},{ 2,"Two","Spades"},{3,"Three","Spades"},{4,"Four","Spades"},{5,"Five","Spades"},{6,"Six","Spades"},{7,"Seven","Spades"},
{ 8,"Eight","Spades"},{ 9,"Nine","Spades"},{10,"Ten","Spades"},{10,"Jack","Spades"},{10,"Queen","Spades"},{10,"King","Spades"}};

エラー

Main.c:39:127: error: initializer-string for array of chars is too long [-fpermissive]

39行目 は、上に掲載したコードの最後の行です

なぜコンパイラがエラーを投げるのか、その原因を突き止めるのに役立ってください。

解決方法を教えてください。

文字列 "Diamonds" は、NULL終端文字を含めて9文字である。したがって name は少なくとも9個の要素を持たなければならない。

しかし、どうやらあなたの name というメンバーであるべきです。 suit その逆も同様です。