C++Deck和Card类错误,分配错误
C++ Deck and Card Class Error with bad alloc
刚开始在学校学习编码。我们的任务要求我们创建一个有纸牌、牌组和手牌类的纸牌游戏。我现在遇到了一些问题,并且在内存位置不断得到异常:std::bad_alloc。这是我现在的代码
卡片类型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;
}
}
甲板类型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++;
}
主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.dellCard((上一直表现不佳;我真的不知道我做错了什么。
在构造函数中,应该为string
s:使用字符串文字
cardType::cardType(){
value=0;
symbol="?";
special="?";
suit="?";
}
并且在CCD_ 3中使用的CCD_。
deckType::deckType()
{ int index = 0;
int current=0;
deck = new cardType[52];
....
在这里,初始化一个局部变量current
,这将隐藏成员current
。
@drescherjm的评论非常重要:
for ( int j = 1; j <= 13; j++ ) {
那不行。记住c++索引从0开始。
相关文章:
- vector.resize()中的分配错误
- 多个文件的内存分配错误"在抛出 'std :: bad_alloc' what (): std :: bad_alloc 的实例后终止调用" [C++]
- 正在理解智能指针,但出现错误:未分配正在释放的指针
- 如何修复只读对象中成员的错误分配?
- 无法将错误分配给成员函数中"类型的实体"
- 分段错误分配函数结果
- 如何实现,错误分配中止而不是抛出异常
- C++代码错误分配
- 如何修复错误分配C++运行时错误
- 分段错误 - 分配类困难
- 带有数组类型表达式的错误分配
- Sprintf 缓冲区问题,错误分配给字符数组
- 而 (p=strtok(NULL, "," )) 警告:p可识别的错误分配
- 错误:分配只读位置“arr2.IntArray::operator[](1)”arr2[1]=24;
- std::映射错误分配错误
- 为什么"e.what()"打印"错误分配"?
- std::vector<const char *>的错误分配效果?
- 使用指向类的指针进行错误分配
- 错误:分配只读引用“__b”
- 向量错误分配错误 C++