-4

我正在尝试编写一个程序来洗牌并处理一副纸牌,不幸的是,我不断收到“矢量下标超出范围”错误。我相信它在 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

卡片.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;
}

主文件

#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

任何建议都非常感谢!

4

1 回答 1

1

问题是在DeckOfCards类中,deck向量永远不会被添加,所以它是空的。

在构造函数中,分配newDeckdeck

deck = newDeck;

构造函数中还有另一个问题:您声明newDeck包含 52 个条目,但随后您使用push_back向向量添加新条目。这会增加向量的大小,因为这些条目是在您声明它最初拥有的 52 个条目之后添加的。

只需使用下标运算符来设置这些条目。

于 2013-03-05T07:10:12.533 回答