刚开始学习在学校编码。我们的任务要求我们创建一个卡牌,牌组和手牌的纸牌游戏。我现在遇到了麻烦,我一直在异常:std :: bad_alloc在内存位置。这是我现在的代码
CardType h:
#ifndef cardType_h
#define cardType_h
#include <string>
using namespace std;
class cardType{
public:
void print();
int getValue() const;
string getSymbol() const;
string getSpecial() const;
string getSuit() const;
int checkSpecial(int gscore) const;
cardType();
cardType(string suit,int value);
private:
int value;
string special;
string symbol;
string suit;
};
#endif
CardType cpp:
#include "cardType.h"
#include <iostream>
#include <string>
using namespace std;
void cardType::print()
{
cout << getSymbol() << " of " << getSuit() << ", having the value of " << getValue() << "."<< endl <<"This card's special is " << getSpecial() << endl;
}
int cardType::getValue() const
{
return value;
}
string cardType::getSymbol() const
{
return symbol;
}
string cardType::getSpecial() const
{
return special;
}
string cardType::getSuit() const
{
return suit;
}
cardType::cardType(){
value=0;
symbol="?";
special='?';
suit='?';
}
cardType::cardType(string s, int v){
suit = s;
value = v;
switch(v){
case 1: // Ace cards have a value of 1 and have no special type
symbol="Ace";
special="None";
break;
case 2: // 2 cards have a value of 2 and have no special type
symbol="2";
special="None";
break;
case 3:
symbol="3"; // 3 cards have a value of 3 and have no special type
special="None";
break;
case 4:
symbol="4"; // 4 cards have a value of 0 and have a special type "Reverse" which reverses the flow of the game
special="Reverse";
value=0;
break;
case 5:
symbol="5"; // 5 cards have a value of 5 and have no special type
special="None";
break;
case 6:
symbol="6"; // 6 cards have a value of 6 and have no special type
special="None";
break;
case 7:
symbol="7"; // 7 cards have a value of 7 and have no special type
special="None";
break;
case 8:
symbol="8"; // 8 cards have a value of 8 and have no special type
special="None";
break;
case 9:
symbol="9"; // 9 cards have a value of 0 and have a special type "Pass" which does not add any value to the game and lets the player skip his turn.
special="Pass";
value=0;
break;
case 10:
symbol="10"; // 10 cards have a value of 10 and have a special type "subtract" which instead of adding the 10 value to the total game it is subtracted instead.
special="Subtract";
value=10;
break;
case 11: // Jack cards have a value of 10 and have no special type
symbol="Jack";
special="None";
value=10;
break;
case 12: // Queens cards have a value of 10 and have no special type
symbol="Queen";
special="None";
value=10;
break;
case 13:
symbol="King"; // King cards have a value of 0 and have a special type "NinetyNine" which changes the total game score to 99 reguardless what number it was previously
special="NinetyNine";
value=0;
break;
}
}
int cardType::checkSpecial(int gscore) const{
if(special=="Pass"){
return gscore;
}
if(special=="Reverse"){
return gscore;
}
if(special=="Subtract"){
return gscore - value;
}
if(special=="NinetyNine"){
return 99;
}
else{
return gscore + value;
}
}
DeckType h:
#ifndef deckType_h
#define deckType_h
#include "cardType.h"
#include <string>
using namespace std;
class deckType
{
public:
void shuffle();
cardType dealCard();
deckType();
private:
cardType *deck;
int current;
};
#endif
DeckType cpp:
#include <iostream>
#include "deckType.h"
using namespace std;
deckType::deckType()
{ int index = 0;
int current=0;
deck = new cardType[52];
string suit[] = {"Hearts","Diamonds","Clubs","Spades"};
int value[] = {1,2,3,4,5,6,7,8,9,10,11,12,13};
for ( int i = 0; i <= 3; i++ ) {
for ( int j = 1; j <= 13; j++ ) {
deck[index] = cardType(suit[i],value[j]);
index++;
}
}
}
cardType deckType::dealCard()
{
return deck[current];
current++;
}
Main cpp:
#include "deckType.h"
#include <iostream>
using namespace std;
int main()
{
deckType gamedeck;
cout << "1" <<endl;
cardType currentCard;
cout << "2" <<endl;
currentCard = gamedeck.dealCard();
cout << "3" <<endl;
return 0;
}
我一直在currentCard = gamedeck.dealCard()中获得bad_alloc; 我真的不知道我做错了什么。
答案 0 :(得分:1)
在构造函数中,您应该为string
s:
cardType::cardType(){
value=0;
symbol="?";
special="?";
suit="?";
}
和current
中使用的dealCard
可能尚未初始化。
deckType::deckType()
{ int index = 0;
int current=0;
deck = new cardType[52];
....
在这里,您初始化一个局部变量current
,这会隐藏成员current
。
@drescherjm的评论非常重要:
for ( int j = 1; j <= 13; j++ ) {
这无法奏效。记住c ++ 索引从0开始。