分配对象数组动态冻结

Allocating array of objects dynamically freeze

本文关键字:冻结 动态 数组 对象 分配      更新时间:2023-10-16

这是在你告诉我不要使用数组之前的赋值。不幸的是,我不得不这么做。我花了很长时间来解决这个问题,最后我放弃了,来问这里的聪明人。我知道这与我分配数组的方式有关,但我不知道哪里出了问题。它通过一次内循环,然后在第二次运行时冻结。抱歉我漏掉了什么重要的东西。我会补充任何需要的信息。

Card中的变量

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 setn";
    setValue(cValue);
    cout << "val setn";
    setSuit(suitNum);
    cout << "suit setn";
    setRank(cRank);
    cout << "rank setn";
}

重载& 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 setn";
    setValue(cValue);
    cout << "val setn";
    setSuit(suitNum);
    cout << "suit setn";
    setRank(cRank);
    cout << "rank setn";
}
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;
}

您遇到麻烦的一个可能原因是您正在解引用NULL指针:

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

cardrunk为NULL。现在尝试解引用一个NULL指针。除非我错过了什么,在哪里调用"cardrunk = new std::string;"?

但这引出了一个更大的问题——在你的评论中,你说你的教授希望你使用指针。但老实说,没有任何理由为这些字符串成员使用指针——绝对没有。

你确实需要指针来实现你的动态数组,但仅此而已。要么你误解了教授的意图,要么教授需要另谋高就。

您将指针传递给指针作为值:initArray(Card **cPtr),显然您的意图是改变它(即返回分配的Card数组)。

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

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

使用字符串的第二个问题,将string *cardRank更改为string cardRank,也可以,您不需要指针指向字符串(不像char*,这些都是对象,赋值应该从*cardRank = "Ace"更改为cardRank = "Ace"