我正在使用类编写代码,并且得到了错误的输出,这是我的函数定义:
void PrintCard(int c)
{
int Rank = c%13;
int Suit = c/13;
const char NameSuit[5] = "SCDH";
const char NameRank[14] = "23456789XJQKA";
cout << NameRank[Rank] << NameSuit[Suit];
}
CardSet::CardSet()
{
Card = NULL;
nCards = 0;
}
CardSet::CardSet(int c)
{
Card = new int[c];
for(int i = 0; i > c; i++)
{
Card[i] = (i % 52);
}
}
CardSet::~CardSet()
{
delete[] Card;
}
bool CardSet::IsEmpty() const
{
return nCards == 0;
}
void CardSet::Print() const
{
for(int i=0; i > nCards; i++)
{
PrintCard(i);
}
}
int CardSet::Size() const
{
return nCards;
}
这是我的主要
cout << "Testing constructors, Print(), Size() & IsEmpty():" << endl;
CardSet CardSet1; // empty cCardSet
CardSet CardSet2(12); // CardSet with 12 cards
if(CardSet1.IsEmpty()) cout<<"CardSet1 is empty"<<endl;
else cout<<"CardSet1 has "<< CardSet1.Size() <<" cards" << endl;
if(CardSet2.IsEmpty()) cout<<"CardSet2 is empty"<<endl;
else cout<<"CardSet2 has "<< CardSet2.Size() <<" cards" << endl;
cout << "Printout of CardSet1: ";
CardSet1.Print();
cout << "Printout of CardSet2: ";
CardSet2.Print();
cout << endl;
当我编译时,我为cardset1得到了正确的值(0),但是为cardset2得到了正确的值(0),而不是输出12的值,这应该是输出,因为我每次编译时都会得到非常高的数字。我认为for循环或内存分配有问题。
这也是类定义的样子:
class CardSet
{
public:
CardSet();
CardSet(int);
~CardSet();
int Size() const;
bool IsEmpty() const;
void Shuffle();
int Deal();
void Deal(int,CardSet&,CardSet&);
void Deal(int,CardSet&,CardSet&,CardSet&,CardSet&);
void AddCard(int);
void MergeShuffle(CardSet&);
void Print() const;
private:
int* Card;
int nCards;
};
任何帮助将不胜感激!
欢呼
答案 0 :(得分:2)
void CardSet::Print() const
{
for(int i=0; i > nCards; i++)
{
PrintCard(i);
}
}
必须
void CardSet::Print() const
{
for(int i=0; i < nCards; i++)
{
PrintCard(i);
}
}
更正最终测试,并且您在CardSet::CardSet(int c)
中遇到了相同的问题,
CardSet::CardSet(int c)
{
nCards = c;
Card = new int[c];
for(int i = 0; i < c; i++)
{
Card[i] = (i % 52);
}
}
其中还必须设置 nCards 。
在 for 中,测试指示循环是否继续,而不是是否结束
for (inits; test; changes) ...
等同于
init;
while (test) {
...
changes;
}
此外, PrintCard 中没有分隔符,因此cout << NameRank[Rank] << NameSuit[Suit];
可能是您还需要在 Print 中添加空格之类的东西:
void CardSet::Print() const
{
for(int i=0; i < nCards; i++)
{
PrintCard(i);
cout << ' ';
}
}
或 PrintCard 中的两个字段也可以分隔
cout << NameRank[Rank] << ' ' << NameSuit[Suit] << endl;
请注意,您可以简化
const char NameSuit[5] = "SCDH";
const char NameRank[14] = "23456789XJQKA";
cout << NameRank[Rank] << NameSuit[Suit];
成为
cout << "23456789XJQKA"[Rank] << "SCDH"[Suit];
或者如果您真的想拥有数组,我建议您不要提供大小,这样可以避免如果更改文字字符串而忘记也更改大小的情况,那么
const char NameSuit[] = "SCDH";
const char NameRank[] = "23456789XJQKA";
例如具有:
#include <iostream>
using namespace std;
class CardSet
{
public:
CardSet();
CardSet(int);
~CardSet();
int Size() const;
bool IsEmpty() const;
void Shuffle();
int Deal();
void Deal(int,CardSet&,CardSet&);
void Deal(int,CardSet&,CardSet&,CardSet&,CardSet&);
void AddCard(int);
void MergeShuffle(CardSet&);
void Print() const;
private:
int* Card;
int nCards;
};
void PrintCard(int c)
{
int Rank = c%13;
int Suit = c/13;
cout << "23456789XJQKA"[Rank] << ' ' << "SCDH"[Suit] << endl;
}
CardSet::CardSet()
{
Card = NULL;
nCards = 0;
}
CardSet::CardSet(int c)
{
nCards = c;
Card = new int[c];
for(int i = 0; i < c; i++)
{
Card[i] = (i % 52);
}
}
CardSet::~CardSet()
{
delete[] Card;
}
bool CardSet::IsEmpty() const
{
return nCards == 0;
}
void CardSet::Print() const
{
for(int i=0; i < nCards; i++)
{
PrintCard(i);
}
}
int CardSet::Size() const
{
return nCards;
}
int main(void)
{
CardSet cs(5);
cs.Print();
}
编译和执行:
pi@raspberrypi:/tmp $ g++ -pedantic -Wall -Wextra c.cc
pi@raspberrypi:/tmp $ ./a.out
2 S
3 S
4 S
5 S
6 S
pi@raspberrypi:/tmp $
答案 1 :(得分:2)
在CardSet::CardSet
中更改
for(int i = 0; i > c; i++)
对此
for (int i = 0; i < c; i++)
还可以在CardSet::Print
中更改
for(int i=0; i > nCards; i++)
对此:
for (int i = 0; i < nCards; i++)
最后,将nCards = c;
添加到CardSet::CardSet
。
答案 2 :(得分:0)
您应该查看它(循环)
void CardSet :: Print()const
{
for(int i = 0; i> nCards; i ++)// @@重新考虑
{
打印卡(i);
}
}