分配对象数组动态冻结

时间:2014-03-09 20:55:54

标签: c++ arrays class object

这是在你告诉我不要使用数组之前进行的一项任务。不幸的是,我必须。 我已经麻烦了很长一段时间,我终于放弃了,来问这里的聪明人。我知道这与我如何分配数组有关,但我无法弄清楚是什么问题。它通过内循环一次,然后在第二次运行时冻结。对不起,如果我留下了重要的东西。我将添加任何所需的信息。

卡片的变量

private:
    string *cardRank;
    string *suit;
    int rankNum;
    int value;

问题功能

void initArray(Card **cPtr)
{
    int i;
    int j;
    int index=0;

    cPtr = new Card*[DECK]; //deck is const int 52

for(i=0; i < 4; ++i)
{
    for(j=1; j < 14; ++j)
    {

        cPtr[index] = new Card(j, j, i); //freezes here. does not make  it to the first                               
                                         //function in the constructor
        cout << cPtr[index] << endl;
        ++index;

    }
}
}

构造

Card::Card(int cRank, int cValue, int suitNum)
{
    setRankNum(cRank);
    cout << "rank num set\n";
    setValue(cValue);
    cout << "val set\n";
    setSuit(suitNum);
    cout << "suit set\n";
    setRank(cRank);
    cout << "rank set\n";
}

重载&lt;&lt;

ostream &operator << (ostream &strm, Card &aCard)
{
    strm << aCard.getRank() << " of " << aCard.getSuit();
    return strm;
}

整个计划

#include <string>
#include "Card.h"
using namespace std;

const int DECK = 52;

void initArray(Card **&cPtr);
void shufflePArray(Card **pArray);
void determineHand(Card **pArray);
bool isFlush(Card *hand);
bool isStraight(Card *hand);
bool isFour(Card *hand);
bool isThree(Card *hand);
bool isTwo(Card *hand);
void drawHand(Card *hand, Card **pArray);
void displayHand(Card **hand);

int main()
{
    Card **cArray;
    initArray(cArray);
    cout << "done.";
    shufflePArray(cArray);
    determineHand(cArray);


    delete [] cArray;
    return 0;
}


void initArray(Card **&cPtr)
{
    int i;
    int j;
    int index=0;

    cPtr = new Card*[DECK];

    for(i=0; i < 4; ++i)
    {
        for(j=1; j < 13; ++j)
        { cout << "inner loop " << index << endl;

        cPtr[index] = new Card(j, j, i);
        cout << cPtr[index] << endl;
        ++index;
        }
    }
}
void shufflePArray(Card **pArray)
{
    //code here
}
void determineHand(Card ***pArray)
{
    Card hand[5];
    drawHand(hand, pArray);
    displayHand(pArray);

    if (isFlush(hand) == true)
    {
        if(isStraight(hand) == true)
            cout << "Straight flush!!!" << endl;
        else
            cout << "You got a flush!" << endl;
    }
    else if(isStraight(hand) == true)
    {
        cout << "You got a straight!" << endl;
    }
    else if(isFour(hand) == true)
    {
        cout << "Four of a kind!!!" << endl;
    }
    else if(isThree(hand)==true)
    {
        cout << "Three of a kind!" << endl;
    }
    else if(isTwo(hand) == true)
    {
        cout << "That's a pair";
    }
}
bool isFlush(Card *hand)
{

    int i;
    int match;

    for (i=0; i<5;++i)
    {
        if (hand[0].getSuit() == hand[i].getSuit())
            match++;
    }

    if (match == 5)
       return true;
    else
        return false;
}
bool isStraight(Card *hand)
{
    int match;

    for (int i=0; i<5;++i)
    {
        if (hand[0].getValue() == hand[i].getValue() - 1)
            match++;
    }

    if (match == 5)
       return true;
    else
        return false;
}
bool isFour(Card *hand)
{
    int match = 0;
    for(int i=0; i<5; ++i)
    {
        match=0;
        for(int j=0;j<5;++j)
        {
            if (hand[i] == hand[j])
                match++;
            if (match == 4)
                return true;
            else
                return false;
        }

    }
}
bool isThree(Card *hand)
{
    int match = 0;
    for(int i=0; i<5; ++i)
    {
        match=0;
        for(int j=0;j<5;++j)
        {
            if (hand[i] == hand[j])
                match++;
            if (match == 3)
                return true;
            else
                return false;
        }

    }
}
bool isTwo(Card *hand)
{
    int match = 0;
    for(int i=0; i<5; ++i)
    {
        match=0;
        for(int j=0;j<5;++j)
        {
            if (hand[i] == hand[j])
                match++;
            if (match == 2)
                return true;
            else
                return false;
        }

    }
}

void drawHand(Card *hand, Card **pArray)
{
    for(int i=0; i<5;++i)
     cout << hand[i];
    }
}
    {
        hand[i] = *pArray[i];
    }
}

void displayHand(Card **hand)
{
    Card temp;
    for (int i = 0; i < 5; ++i)
    {

card.h的内容

#ifndef CARD_H
#define CARD_H
#include <string>
#include <iostream>
using namespace std;



class Card
{
    private:
        string *cardRank;
        string *suit;
        int rankNum;
        int value;
    public:
        Card();
        Card(int cRank, int cValue, int suitNum);
        friend ostream &operator << (ostream &strm, Card &aCard);
        bool operator > (const Card &aCard);
        bool operator < (const Card &aCard);
        bool operator == (const Card &aCard);
        void setRank(int r);
        void setSuit(int s);
        void setValue(int v);
        void setRankNum(int n);
        string getRank();
        string getSuit();
        int getRankNum();
        int getValue();
};

#endif // CARD_H

Card.cpp的内容

#include "Card.h"
#include <string>
#include <iostream>

Card::Card()
{
    cardRank = NULL;
    suit = NULL;
    rankNum = 0;
    value = 0;
}


Card::Card(int cRank, int cValue, int suitNum)
{
    setRankNum(cRank);
    cout << "rank num set\n";
    setValue(cValue);
    cout << "val set\n";
    setSuit(suitNum);
    cout << "suit set\n";
    setRank(cRank);
    cout << "rank set\n";
}

ostream &operator << (ostream &strm, Card &aCard)
{
    strm << aCard.getRank() << " of " << aCard.getSuit();
    return strm;
}

bool Card::operator > (const Card &aCard)
{
    if (aCard.value > value)
        return true;
    else
        return false;
}

bool Card::operator < (const Card &aCard)
{
    if (aCard.value < value)
        return true;
    else
        return false;
}

bool Card::operator == (const Card &aCard)
{
    if (value == aCard.value)
        return true;
    else
        return false;
}

void Card::setRank(int r)
{
    switch(r)
    {
    case 13:
        *cardRank = "Ace";
    case 1:
        *cardRank = "Two";
    case 2:
        *cardRank = "Three";
    case 3:
        *cardRank = "Four";
    case 4:
        *cardRank = "Five";
    case 5:
        *cardRank = "Six";
    case 6:
        *cardRank = "Seven";
    case 7:
        *cardRank = "Eight";
    case 8:
        *cardRank = "Nine";
    case 9:
        *cardRank = "Ten";
    case 10:
        *cardRank = "Jack";
    case 11:
        *cardRank = "Queen";
    case 12:
        *cardRank = "King";
    }
}

void Card::setSuit(int s)
{
    if(s==0){
        *suit = "Hearts";
        cout << "suit set";}
    else if(s==1)
        *suit = "Diamonds";
    else if (s==2)
        *suit = "Clubs";
    else if (s==3)
        *suit = "Spades";
    else
        cout << "Invalid suit num.";
}
void Card::setValue(int v)
{
    if (v > 0)
        value = v;
}
void Card::setRankNum(int n)
{
    rankNum = n;
}
string Card::getRank()
{
    return *cardRank;
}
string Card::getSuit()
{
    return *suit;
}
int Card::getRankNum()
{
    return rankNum;
}
int Card::getValue()
{
    return value;
}

2 个答案:

答案 0 :(得分:2)

您遇到麻烦的一个可能原因是您要取消引用NULL指针:

void Card::setRank(int r)
{
    switch(r)
    {
        case 13:
          *cardRank = "Ace";
        case 1:
          *cardRank = "Two";
     //...

cardRank为NULL。您现在尝试取消引用NULL指针。除非我错过了什么,否则调用“cardRank = new std :: string;”?

但这会带来更大的一点 - 在你的评论中,你说你的教授希望你使用指针。但老实说,没有理由为这些字符串成员使用指针 - 绝对没有。

你需要指针来实现动态数组,但就是这样。你是在正确地阅读教授的意图,还是教授需要另一个职业。

答案 1 :(得分:1)

你在指针中传递指针作为值initArray(Card **cPtr),显然你打算改变它(即返回分配的Card数组。

我无法确切地说出发生了什么但是有些内存会被覆盖,堆栈中的返回地址可能

无论如何要么将其更改为引用,即initArray(Card **&cPtr)或使用initArray(Card ***cPtr)并相应地更改代码。

使用字符串的第二个问题,将string *cardRank更改为string cardRank并且也适合,您不需要指向字符串的指针(它不像char*,这些是对象无论如何,分配应从*cardRank = "Ace"更改为cardRank = "Ace"