我需要帮助检查我的代码。任何评论都非常感谢
I'm in need of help checking my code. Any reviewing is greatly apreciated
这是我的老师提供的驱动程序代码,我不打算编辑。
PlayingCardTest.cpp :
#include <iostream>
#include "PlayingCard.h"
PlayingCard makeValidCard(int value, int suit);
int main()
{
// Create a playing card
PlayingCard card1;
// Test the default constructor and GetCardCode
std::cout << "Testing default constructor. Expect card code to be 00n card code is :";
std::cout << card1.getCardCode() << std::endl << std::endl;
// Test the setter and getter
std::cout << "Seting card to 'AH' using SetValue and SetSuit" << std::endl;
card1.setCard('A', 'H');
std::cout << "GetValue returns :" << card1.getValue() << std::endl;
std::cout << "GetSuit returns :" << card1.getSuit() << std::endl << std::endl;
// Test overloaded constructor
PlayingCard tenOfSpades('T', 'S');
std::cout << "Testing overloaded constructor. Expect card code to be TSn card code is :";
std::cout << tenOfSpades.getCardCode() << std::endl << std::endl;
// Test IsValid with valid cards
std::cout << "Testing valid card codes.n"
<< "Expect isValid to return true for all (except perhaps Jokers.)"
<< std::endl;
// Create and test valid cards
int validCards = 0; // cards that return true for IsValid
int invalidCards = 0; // cards that return false for IsValid
// Create and test four suits plus the jokers
for(int suit = 1; suit <= 5; suit++)
{
// Create and test ace, 2 - 9, Jack, Queen, and King
for(int value = 1; value <= 13; value++)
{
PlayingCard aCard = makeValidCard(value, suit);
std::cout << "Card Code: " << aCard.getCardCode() << " IsValid :";
if (aCard.isValid())
{
validCards++;
std::cout << "true" << std::endl;
}
else
{
invalidCards++;
std::cout << "false" << std::endl;
}
// suit 5 is just for creating the two Jokers
if (suit == 5 && value >= 2)
break;
}
}
std::cout << "IsValid returned false for " << invalidCards << " card codes" << std::endl;
std::cout << "IsValid returned true for " << validCards << " card codes" << std::endl;
std::cout << std::endl;
// Test IsValid with invalid cards
// Create and test invalid cards
std::cout << "Testing invalid card codes; isValid should return false for all." << std::endl;
validCards = 0;
invalidCards = 0;
// Loop through all possible ASCII character codes for card codes
for(int suit = 0; suit <= 255; suit++)
for(int value = 0; value <= 255; value++)
{
// Only check card codes that are not valid
PlayingCard aCard = makeValidCard(value, suit);
if (aCard.getCardCode() == "00")
{
if (aCard.isValid())
{
std::cout << "value :" << value << " suit :" <<suit << " IsValid :";
std::cout << "true" << std::endl;
validCards++;
}
else
{
invalidCards++;
}
}
}
std::cout << "IsValid returned false for " << invalidCards << " card codes" << std::endl;
std::cout << "IsValid returned true for " << validCards << " card codes" << std::endl;
return 0;
}
/******************************************************/
/* Test Functions */
/******************************************************/
PlayingCard makeValidCard(int iValue, int iSuit)
{
char value = '0';
char suit = '0';
switch (iValue)
{
case 1:
value = 'A';
break;
case 10:
value = 'T';
break;
case 11:
value = 'J';
break;
case 12:
value = 'Q';
break;
case 13:
value = 'K';
break;
default:
if ((iValue >= 2) && (iValue <= 9))
value = '0' + iValue;
break;
}
switch (iSuit)
{
case 1:
suit = 'D';
break;
case 2:
suit = 'S';
break;
case 3:
suit = 'C';
break;
case 4:
suit = 'H';
break;
// Special case for the Joker
case 5:
if(iValue == 1)
{
value = 'Z';
suit = 'B';
}
else if(iValue == 2)
{
value = 'Z';
suit = 'R';
}
else
{
value = '0';
suit = '0';
}
break;
}
PlayingCard testCard(value, suit);
return testCard;
}
这是我的头文件PlayingCard.h:
#ifndef PLAYINGCARD_H_INCLUDED
#define PLAYINGCARD_H_INCLUDED
class PlayingCard
{
private:
char suit, value;
public:
PlayingCard(){suit = '0'; value = '0';}
PlayingCard(char myValue, char mySuit);
char getValue() {return value;}
char getSuit() {return suit;}
std::string getCardCode();
bool setCard(char myValue, char mySuit);
bool isValid();
#endif // PLAYINGCARD_H_INCLUDED
这是我的类实现文件 playingcard。cpp:
#include "PlayingCard.h"
PlayingCard::PlayingCard (char myValue, char mySuit)
{
char aValue[13] ('2','3','4','5','6','7','8','9','T','J','Q','K','A'))
char aSuit[4] {'D','H','C','S']
for(count = 0; count <= 12; count++)
{
if (myValue = aValue[count])
{
for (count2 = 0; count2 <= 3; count2++)
{
if (mySuit = aSuit[count2++])
{
suit = mySuit;
value = myValue;
}
}
}
}
}
bool PlayingCard::setCard(char myValue, char mySuit)
{
char aValue[13] ('2','3','4','5','6','7','8','9','T','J','Q','K','A'))
char aSuit[4] {'D','H','C','S']
for(count = 0; count <= 12; count++)
{
if (myValue = aValue[count])
{
for (count2 = 0; count2 <= 3; count2++)
{
if (mySuit = aSuit[count2++])
{
suit = mySuit;
value = myValue;
}
else
{
return false;
}
}
}
}
}
string PlayingCard::getCardCode()
{
return suit + value;
}
bool PlayingCard::isValid()
{
char aValue[13] ('2','3','4','5','6','7','8','9','T','J','Q','K','A'))
char aSuit[4] {'D','H','C','S']
for(count = 0; count <= 12; count++)
{
if (myValue = aValue[count])
{
for (count2 = 0; count2 <= 3; count2++)
{
if (mySuit = aSuit[count2++])
{
return true;
}
else
{
return false;
}
}
}
}
}
这是我得到的编译器错误。我不知道该怎么办,看起来它们在我不应该编辑的文件中。我很感激你能给予的帮助。
PlayingCardTest.cpp|103|错误:'PlayingCard PlayingCard::makeValidCard(int, int)'无法重载|
PlayingCardTest.cpp|5|error: with 'PlayingCard PlayingCard::makeValidCard(int, int)'|
PlayingCardTest.cpp|169|错误:期望'}'在输入结束|
PlayingCardTest.cpp|169|error: expected unqualified-id at end of input|
||===构建完成:4个错误,0个警告===|
您的头文件末尾缺少};
第一轮评论:
- Style unit:顺序section为"public","protected",然后是"private"。私人部门不应该先于公共部门。这在技术上不是必需的,但却是相当标准的做法。
- Style nit:使用单独的语句声明每个变量,每个变量在自己的行上。使用逗号是陷入麻烦的好方法(例如在声明指针类型时),并且是糟糕的风格。
- 在构造函数中使用初始化列表,而不是使用赋值操作符。
- 你应该在你的头文件中包含"
"来使用std::string。
第二轮评论:
- 你在奇怪地初始化数组;你应该使用{}作为括号。
- 初始化时不需要指定数组的大小。
- Style nit:不要在你的代码中使用像"12"这样的神奇常数。相反,将它们赋值给一个变量,如value_length或value_count,并使用已命名的变量。
- 你的意思是做一个相等比较("==")或赋值("=")在你的if语句?如果你想做一个赋值,你应该把它移出If。
第三轮评论:
- 在非默认构造函数和setCard函数之间不必要地重复代码。您应该能够在这两个函数之间共享代码。因为setCard不是一个虚函数,所以你应该能够简单地从构造函数中调用它。你的setCard逻辑似乎相当复杂。大多数"集合"函数都比这简单得多。您应该考虑添加文档来解释它试图做的事情的逻辑。
- "getValue()"、"getCardCode()"、"getSuit()"answers"isValid()"函数应声明为"const"。
第四轮评语:
- 既然你的教授做"PlayingCard card = makeValidCard(....)",很明显,他希望你的卡类支持分配。因为你的"setCard()"函数和你的非默认构造函数做一些事情,而不是简单地设置属性,这将是有意义的提供一个"PlayingCard&operator=(const PlayingCard&);"赋值操作符以及"PlayingCard::PlayingCard(const PlayingCard&)"复制构造函数。如果您没有提供这些,最好添加注释,说明使用默认赋值/copy进行复制是有意允许的。
相关文章:
- 如果C++类在类方法中具有动态分配,但没有构造函数/析构函数或任何非静态成员,那么它仍然是POD类型吗
- 奇怪的(对我来说)返回声明 - 在谷歌上找不到任何关于它的信息
- 如何在不产生任何垃圾的情况下获得C中的像素
- 为什么我不能在 C++ 中的特定函数重载中调用同一函数的任何其他重载?
- C++映射有2个键,这样任何1个键都可以用来获取值
- Visual Studio(或任何其他工具)能否将地址解释为调用堆栈(boost上下文)的开头
- RtlCaptureStackBackTrace未捕获任何帧
- 链表c++插入,所有情况都已检查,但没有任何工作
- C++模板函数,用于比较任何无符号整数和有符号整数
- 如何以叮当格式设置评论的行长?
- Arduino millis() - millis() 怎么能等于 0 以外的任何东西?
- 尝试摆脱任何堆内存分配
- 是否有任何C++功能可以对地图进行排序?
- 打印时有二叉树问题.用户输入不打印任何内容
- 共享指针和具有自定义删除程序的唯一指针之间的语法差异背后的任何原因
- 在C++行尾写一个分号或多个分号是否会改变任何内容
- C++类型特征,以查看是否可以<uint32_t>对类型"K"的任何变量调用"static_cast(k)"
- C++ 将 CIN 值存储到任何类型的数组中
- 澄清任何声明中的评论
- 我需要帮助检查我的代码。任何评论都非常感谢