C++ 我想从这段代码中删除using namespace std,但我不确定所有这些都需要以std为前缀:
我想使用namespace std删除,但我不知道需要加什么前缀C++ 我想从这段代码中删除using namespace std,但我不确定所有这些都需要以std为前缀:,c++,namespaces,C++,Namespaces,我想使用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 constructo
#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;
}
#包括
#包括“PlayingCard.h”
使用名称空间std;
PlayingCard makeValidCard(整数值,整数套装);
int main()
{
//制作一张扑克牌
玩牌卡1;
//测试默认构造函数和GetCardCode
cout我看到的仅有两个是cout
和endl
其他所有内容都是关键字或用户定义的。只需删除它,然后在编译器出错的所有内容前加前缀。:p
快速浏览一下,肯定需要加前缀的是cout
和endl
另外,请不要使用endl
仅插入换行符,请使用普通的旧'\n'
。endl
不仅插入换行符,而且刷新流,如果频繁使用(您这样做),这可能会对性能造成相当大的损失。仅当您需要刷新流并插入换行符时才使用它。我没有仔细检查代码,但可能只是cout
和endl
。如果编译器给您一个关于未定义符号的错误,请尝试将std添加到该代码中。顺便说一句,您也可以使用命名空间s更改td;
到使用std::cout;
和使用std::endl;
,这样可以防止全局名称空间被过度污染,但避免将限定符添加到代码中的每个实例。鉴于使用cout
的频率,您可能希望将使用名称空间std;
替换为使用std::cout;
正如Xeo所说,您应该用'\n'
搜索并替换endl
看起来可能覆盖所有的基础。通过出现“代码”> STD::/CUT>出现其他编译器错误。
短版本“几乎是来自C++标准库的任何东西”。C标准库不在STD命名空间中,也不是原始类型(int,char等)。在你的例子中,你从C++标准库中使用的主要东西是CUT和EnDL,它需要用STD::LOL前缀——你刚刚展示了为什么在源文件中使用<代码>使用命名空间< /代码>是一个非常坏的主意。“我怎么知道名字属于什么名字空间?”+1:-)在这样一个简单的控制台程序中使用endl不会导致性能下降。大多数类似的程序都会花费99.9%的时间等待cin。