2013-03-05 185 views
-4

我想写一个程序,将洗牌并处理一副扑克牌,不幸的是,我不断收到“Vector下标出界”错误。我相信它在DeckOfCards.cpp文件中,但我似乎无法弄清楚它为什么抛出这个错误或如何纠正它。反正这里是代码:向量下标超出范围C++

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 

任何建议都非常感谢!

+3

你*相信*错误在那里?请使用调试器并检查代码实际失败的位置。 – Zeta 2013-03-05 07:05:11

+0

错误应该告诉你它到底发生了什么。 – 2013-03-05 07:05:16

+0

而不是有一个整数索引的套装和面,考虑使用枚举。 – 2013-03-05 07:05:27

回答

1

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

在构造函数中,分配newDeckdeck

deck = newDeck; 

还有一个在构造函数中的另一个问题:你申报newDeck包含52项,但你用push_back来添加新条目矢量。这增加矢量的大小添加这些条目后您声明它最初有52个条目。

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