我想从此代码中删除 using namespace std,但不确定所有内容都需要以 std:: 为前缀
我想删除 using namespace std,但我不知道所有内容都需要添加前缀。
#include <iostream>
#include "PlayingCard.h"
using namespace std;
PlayingCard makeValidCard(int value, int suit);
int main()
{
// Create a playing card
PlayingCard card1;
// Test the default constructor and GetCardCode
cout << "Testing default constructor. Expect card code to be 00\n card code is :";
cout << card1.getCardCode() << endl << endl;
// Test the setter and getter
cout << "Seting card to 'AH' using SetValue and SetSuit" << endl;
card1.setCard('A', 'H');
cout << "GetValue returns :" << card1.getValue() << endl;
cout << "GetSuit returns :" << card1.getSuit() << endl << endl;
// Test overloaded constructor
PlayingCard tenOfSpades('T', 'S');
cout << "Testing overloaded constructor. Expect card code to be TS\n card code is :";
cout << tenOfSpades.getCardCode() << endl << endl;
// Test IsValid with valid cards
cout << "Testing valid card codes.\n"
<< "Expect isValid to return true for all (except perhaps Jokers.)"
<< 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);
cout << "Card Code: " << aCard.getCardCode() << " IsValid :";
if (aCard.isValid())
{
validCards++;
cout << "true" << endl;
}
else
{
invalidCards++;
cout << "false" << endl;
}
// suit 5 is just for creating the two Jokers
if (suit == 5 && value >= 2)
break;
}
}
cout << "IsValid returned false for " << invalidCards << " card codes" << endl;
cout << "IsValid returned true for " << validCards << " card codes" << endl;
cout << endl;
// Test IsValid with invalid cards
// Create and test invalid cards
cout << "Testing invalid card codes; isValid should return false for all." << 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())
{
cout << "value :" << value << " suit :" <<suit << " IsValid :";
cout << "true" << endl;
validCards++;
}
else
{
invalidCards++;
}
}
}
cout << "IsValid returned false for " << invalidCards << " card codes" << endl;
cout << "IsValid returned true for " << validCards << " card codes" << 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;
}
I want to remove using namespace std, but I don't know what all needs to be prefixed.
#include <iostream>
#include "PlayingCard.h"
using namespace std;
PlayingCard makeValidCard(int value, int suit);
int main()
{
// Create a playing card
PlayingCard card1;
// Test the default constructor and GetCardCode
cout << "Testing default constructor. Expect card code to be 00\n card code is :";
cout << card1.getCardCode() << endl << endl;
// Test the setter and getter
cout << "Seting card to 'AH' using SetValue and SetSuit" << endl;
card1.setCard('A', 'H');
cout << "GetValue returns :" << card1.getValue() << endl;
cout << "GetSuit returns :" << card1.getSuit() << endl << endl;
// Test overloaded constructor
PlayingCard tenOfSpades('T', 'S');
cout << "Testing overloaded constructor. Expect card code to be TS\n card code is :";
cout << tenOfSpades.getCardCode() << endl << endl;
// Test IsValid with valid cards
cout << "Testing valid card codes.\n"
<< "Expect isValid to return true for all (except perhaps Jokers.)"
<< 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);
cout << "Card Code: " << aCard.getCardCode() << " IsValid :";
if (aCard.isValid())
{
validCards++;
cout << "true" << endl;
}
else
{
invalidCards++;
cout << "false" << endl;
}
// suit 5 is just for creating the two Jokers
if (suit == 5 && value >= 2)
break;
}
}
cout << "IsValid returned false for " << invalidCards << " card codes" << endl;
cout << "IsValid returned true for " << validCards << " card codes" << endl;
cout << endl;
// Test IsValid with invalid cards
// Create and test invalid cards
cout << "Testing invalid card codes; isValid should return false for all." << 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())
{
cout << "value :" << value << " suit :" <<suit << " IsValid :";
cout << "true" << endl;
validCards++;
}
else
{
invalidCards++;
}
}
}
cout << "IsValid returned false for " << invalidCards << " card codes" << endl;
cout << "IsValid returned true for " << validCards << " card codes" << 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;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
只需将其删除,然后为编译器出错的所有内容添加前缀即可。 :P
快速浏览一下,肯定需要添加前缀的是
cout
和endl
。另外,请不要使用
endl
来插入换行符,而是使用普通的旧'\n'
。endl
不仅插入换行符,而且还刷新流,如果频繁使用(您确实这样做),这可能会严重影响性能。仅当除了插入换行符之外还需要刷新流时才使用它。Just remove it and then prefix everything that the compiler errors on. :P
With a quick glance, what definitely needs to be prefixed are
cout
andendl
.Also, please do not use
endl
to just insert newlines, use plain old'\n'
instead.endl
not only inserts a newline, but also flushs the stream, which can be quite the performance penalty if used frequently (which you do). Only use it if you need the stream flushed in addition to inserting a newline.我没有仔细查看代码,但可能只是
cout
和endl
。如果编译器给出有关未定义符号的错误,请尝试将 std 也添加到其中。顺便说一句,您还可以将using namespace std;
更改为using std::cout;
和using std::endl;
这将保持全局命名空间避免被过度污染,但避免将限定符添加到代码中的每个实例。I didn't look over the code with a fine tooth comb, but probably just
cout
andendl
. If the compiler gives you an error about an undefined symbol, try adding std to that too. Incidentally, you could also changeusing namespace std;
tousing std::cout;
andusing std::endl;
That would keep the global namespace from being overly polluted, but avoid adding the qualifier to every instance in your code.我看到的唯一两个是
cout
和endl
其他所有内容都是关键字或用户定义的。The only two I see are
cout
andendl
everything else is a keyword or user defined.鉴于您使用
cout
的频率,您可能需要将using namespace std;
替换为using std::cout;
。正如 Xeo 所说,您应该搜索并替换
endl
为'\n'
。看起来这可能涵盖了你所有的基础。通过添加出现的
std::
来修复任何其他编译器错误。Given the frequency that you use
cout
, you might want to replaceusing namespace std;
withusing std::cout;
.As Xeo says, you should search-and-replace
endl
with'\n'
.It looks like that might cover all your bases. Fix any other compiler errors by adding
std::
as they appear.