我正在尝试编写一个程序来洗牌和处理一副牌,不幸的是,我一直收到“向量下标超出界限”的错误。我相信它在DeckOfCards.cpp文件中,但我似乎无法弄清楚为什么它会抛出此错误或如何纠正它。无论如何这里是代码:
Card.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;
}
Main.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
非常感谢任何建议!
答案 0 :(得分:1)
问题是在DeckOfCards
类中,deck
向量永远不会被添加到,所以它是空的。
在构造函数中,将newDeck
分配给deck
:
deck = newDeck;
构造函数中还有另一个问题:您声明newDeck
包含52个条目,但随后您使用push_back
向向量添加新条目。这个增加了向量的大小,因为这些条目是在之后添加的之后的。
只需使用下标运算符来设置这些条目。