[英]Allocating array of objects dynamically freeze
这是在您告诉我不要使用数组之前进行的分配。 不幸的是我必须这样做。 我已经为此困扰了很长时间,终于放弃了,来问这里的聪明人。 我知道这与我如何分配数组有关,但是我不知道出了什么问题。 它经过内部循环一次,然后在第二次运行中冻结。 对不起,如果我遗漏任何重要内容。 我将添加任何需要的信息。
卡中的变量
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";
}
重载<<
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;
}
造成麻烦的一种可能原因是您取消引用了NULL指针:
void Card::setRank(int r)
{
switch(r)
{
case 13:
*cardRank = "Ace";
case 1:
*cardRank = "Two";
//...
cardRank为NULL。 现在,您尝试取消引用NULL指针。 除非我错过了什么,否则对“ cardRank = new std :: string;”的调用在哪里?
但这带来了更大的意义-在您的评论中,您指出您的教授希望您使用指针。 但老实说,没有理由为这些字符串成员使用指针-绝对没有理由。
您确实需要指针来实现您的动态数组,仅此而已。 您或者错误地理解了教授的意图,或者教授需要从事另一职业。
您正在将指向指针的指针作为值传递给initArray(Card **cPtr)
,显然您的意图是更改它(即返回分配的Card
数组)。
我不能真正说出到底发生了什么,但是一些内存被覆盖了,堆栈中的返回地址可能
无论如何,要么将其更改为引用,即initArray(Card **&cPtr)
要么使用initArray(Card ***cPtr)
并相应地更改代码。
使用字符串的第二个问题是,将string *cardRank
更改为string cardRank
并也适合,您不需要指向字符串的指针(这与char*
,无论如何它们都是对象,应将赋值从*cardRank = "Ace"
更改为*cardRank = "Ace"
到cardRank = "Ace"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.