カードのデッキをシャッフルして配るプログラムを作成しようとしていますが、残念ながら、「Vector Subscript Out of Bounds」エラーが発生し続けます。DeckOfCards.cpp ファイルにあると思いますが、このエラーが発生する理由や修正方法がよくわかりません。とにかくここにコードがあります:
カード.h
#ifndef CARD_H
#define CARD_H
#include <string>
using namespace std;
class Card
{
public:
static const int totalFaces = 13; // total number of faces
static const int totalSuits = 4; // total number of suits
Card( int cardFace = 0, int cardSuit = 0 ); // initialize face and suit
string toString() const; // returns a string representation of a Card
// get the card's face
int getFace() const
{
return face;
} // end function getFace
// get the card's suit
int getSuit() const
{
return suit;
} // end function getSuit
private:
int face;
int suit;
static const string faceNames[ totalFaces ];
static const string suitNames[ totalSuits ];
}; // end class Card
#endif
Card.cpp
#include <iostream>
#include "Card.h"
#include "DeckOfCards.h"
using namespace std;
const std::string Card::faceNames[ totalFaces ] = {"Ace", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King"};
const std::string Card::suitNames[ totalSuits ] = {"Hearts", "Clubs", "Diamonds", "Spades"};
Card::Card( int cardFace, int cardSuit )
{
face = cardFace;
suit = cardSuit;
}
string Card::toString() const
{
return faceNames[ face ] + " of " + suitNames[ suit ];
}
DeckOfCards.h
#ifndef DECK_OF_CARDS_H
#define DECK_OF_CARDS_H
#include <vector>
#include "Card.h"
using namespace std;
// DeckOfCards class definition
class DeckOfCards
{
public:
DeckOfCards(); // constructor initializes deck
void shuffle(); // shuffles cards in deck
Card dealCard(); // deals cards in deck
bool moreCards() const; // are there any more cards left
private:
vector< Card > deck; // represents deck of cards
unsigned currentCard; // index of next card to be dealt
}; // end class DeckOfCards
#endif
DeckOfCards.cpp
#include <iostream>
#include <cstdlib>
#include <ctime>
#include "DeckOfCards.h"
#include "Card.h"
using namespace std;
// DeckOfCards default constructor initialized deck
DeckOfCards::DeckOfCards()
{
vector <Card> newDeck (52);
currentCard = 0;
for (int i = 0; i < Card::totalFaces; i++)
{
for (int j = 0; j < Card::totalSuits; j++)
{
newDeck.push_back(newDeck[currentCard] = Card(i,j));
currentCard++;
}
}
}
void DeckOfCards::shuffle() // shuffles cards in deck
{
for (int i = 0; i < 52; i++)
{
int randCard = rand() % 52;
// swaps card with random card
Card swap = deck[i];
deck[i] = deck[randCard];
deck[randCard] = swap;
}
}
Card DeckOfCards::dealCard() // deals cards in deck
{
return deck[currentCard++];
}
bool DeckOfCards::moreCards() const // checks if the current card is out of bounds, if so, no more cards
{
if(currentCard<=52)
return true;
else
return false;
}
メイン.cpp
#include <iostream>
#include <iomanip>
#include "DeckOfCards.h" // DeckOfCards class definition
using namespace std;
int main()
{
DeckOfCards myDeckOfCards;
myDeckOfCards.shuffle(); // place Cards in random order
// print all 52 Cards in the order in which they are dealt
for ( int i = 1; myDeckOfCards.moreCards(); ++i )
{
// deal and display a Card
cout << left << setw( 19 ) << myDeckOfCards.dealCard().toString();
if ( i % 4 == 0 ) // output newline every 4 cards
cout << endl;
} // end for
} // end main
どんな提案でも大歓迎です!