C ++在构造之后无法访问动态分配的卡阵列

时间:2014-04-19 00:00:55

标签: c++ arrays dynamic

这个人现在一直在烦我一段时间。该项目是一个纸牌游戏。

我最初创建了套牌并继续前进,但不得不回来改变它,因为我做错了。 如果从deck.cpp中的任何地方调用它,shuffle()方法就可以正常工作,但是如果我创建了一个Deck * deck;在hand.cpp文件中,然后调用deck-> shuffle()它会挂起并最终崩溃。 我认为这与动态分配Card* deckOfCards

有关

以下代码的控制台输出是:

Shuffle Called
Shuffle Successful
Destructor Called
Shuffle Called

然后它就会挂起并崩溃。 我试过删除析构函数,但这没有帮助...

是否有人能够了解这里有什么问题?任何建议将非常感谢!请参阅下面的代码。

hand.h

#ifndef _hand_h
#define _hand_h

#include "deck.h"

/// COMMENTS HERE
class Hand {
public:
    ///Constructors / Destructors
    Hand();
    ~Hand();

    Deck* deck;
    ///Accessors

    ///Mutators
    void clear();
    void addCard(Card*);
    std::string makeBid();

    ///Operators
    friend std::ostream& operator <<(std::ostream& out, Hand& hand);

private:

    int highCardPoints;
    int lengthPoints;
    bool balancedHand;

protected:
};

#endif // _hand_h

hand.cpp

/**
 * The Hand class is responsible for, among other things, storing the cards dealt to a      * particular player.
 */
#include <vector>

#include "hand.h"

std::vector<Card*> suitClubs;
std::vector<Card*> suitDiamonds;
std::vector<Card*> suitHearts;
std::vector<Card*> suitSpades;

///Constructors / Destructors
Hand::Hand(){

    Deck();
    deck->shuffle();
}

Hand::~Hand(){
}

///Mutators
void Hand::clear(){

}

加入deck.h

#ifndef _deck_h
#define _deck_h

#include "card.h"
#include <iostream>
/**
 * Declare unchanging numbers that relate to the number of ranks and suits possible
 * Prevents use of magic numbers while iterating for creation of all cards
 */
const static int RANKS = 13;
const static int SUITS = 4;
const static int DECK_SIZE = RANKS * SUITS;

class Deck{
public:

    ///Constructors / Destructors
    Deck();
    ~Deck();

    Card* deckOfCards[DECK_SIZE + 1];
    //Card **deckOfCards;
    Card* tempCard;


    ///Accessors


    ///Mutators
    void reset(); ///Sets cardsDealt to 0;
    Card* dealNextCard(); ///Gets the next card from the deck
    void shuffle(); ///Shuffles the cards in the deck

    friend ostream& operator<<(std::ostream&, Deck&); ///Sends a string representation of the cards in the deck to the output stream.
    friend std::istream& operator>>(std::istream&, Deck&);


private:
    int deckIndex, cardsDealt;
    int tempCardIndex = DECK_SIZE + 1;

protected:

};

#endif // _deck_h

deck.cpp

#include "deck.h"
#include "random.h"

#include <vector>
#include <iostream>

Random randomizer;


///Noarg constructor – Creates a dynamic array of Card objects and initialises them. Initialises cardsDealt to 0.
Deck::Deck(){
    *deckOfCards = new Card[DECK_SIZE];
    tempCard = new Card();
    cardsDealt, deckIndex = 0;

    ///Iterate through suits
    for (int suitIterator = 0; suitIterator < SUITS; suitIterator++){

        ///Iterate through all possible ranks for each suit
        for (int rankIterator = 0; rankIterator < RANKS; rankIterator++){
            ///Dynamically create a new card for each rank and suit at the top of the deck
            deckOfCards[deckIndex] = new Card(static_cast<Rank>(rankIterator), static_cast<Suit>(suitIterator));

        //cout << deckOfCards[deckIndex]->toString() << endl;
            deckIndex++;
        }
    }

    shuffle();
}


Deck::~Deck(){
    delete [] deckOfCards;
    delete [] tempCard;
    cout << "Destructor called" << endl;
} ///Destructor - deletes the dynamic allocations

///Sets cardsDealt back to zero
void Deck::reset(){
    cardsDealt = 0;
}

///Returns a pointer to the next Card object from the deck, increments cardsDealt
Card* Deck::dealNextCard(){
    *tempCard = *deckOfCards[cardsDealt];
    cardsDealt++;
    return tempCard;
}

///Shuffles all cards in the deck
void Deck::shuffle(){

    cout << "Shuffle called" << endl;
    int shuffleIndex = 0;
    ///Reset shuffleIndex back to zero just in case it's accessed before the program finishes and still holds a value
    /// Iterate through and shuffle all cards
    /// Algorithm: For each iteration, pick a random card in the deck, swap card at iterated index with randomly chosen card
    for (int i = 0; i < DECK_SIZE; i++){
        shuffleIndex = randomizer.randomInteger(0, (DECK_SIZE - 1));

        *deckOfCards[tempCardIndex] = *deckOfCards[shuffleIndex];
        *tempCard = *deckOfCards[shuffleIndex];

        deckOfCards[shuffleIndex] = deckOfCards[i];
        *deckOfCards[i] = *tempCard;
    }
    cout << "Shuffle successful" << endl;
}

Card对象具有RANK和SUIT属性。如果需要,我也可以提供。

1 个答案:

答案 0 :(得分:4)

Hand::deck是一个永远不会初始化的指针。 Deck();构造函数中的调用Hand会创建一个Deck的匿名实例,该实例会在deck仍未指向任何内容时立即再次销毁。请尝试使用deck=new Deck;代替Deck();