我想从这段代码中删除使用命名空间std,但我不确定什么都需要用std::作为前缀
I would like to remove using namespace std from this code, but am unsure what all needs to be prefixed with std::
我想使用命名空间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 00n 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 TSn 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;
}
删除它,然后在编译器错误的地方加上前缀。: P
快速浏览一下,肯定需要加前缀的是cout
和endl
。
此外,请不要使用endl
来插入换行符,而是使用普通的旧'n'
。endl
不仅插入换行符,而且还刷新流,如果频繁使用(您确实这样做了),这可能会造成相当大的性能损失。只有当您需要除插入换行符外刷新流时才使用它。
我没有仔细检查代码,但可能只是cout
和endl
。如果编译器给你一个关于未定义符号的错误,试着把std也加进去。顺便说一下,您还可以将using namespace std;
更改为using std::cout;
和using std::endl;
,这将防止全局名称空间被过度污染,但避免在代码中的每个实例中添加限定符。
我只看到两个cout
和endl
,其他都是关键字或用户定义的
考虑到您使用cout
的频率,您可能希望将using namespace std;
替换为using std::cout;
。
正如Xeo所说,您应该用'n'
搜索并替换endl
。
看起来这可能涵盖了你所有的基础。
std::
来修复任何其他编译器错误。
相关文章:
- 这个语法std::class<>{}(arg1, arg2) 在C++中是什么意思?
- "using namespace std;"在C++的作用是什么?
- 传递给std::function template的template参数究竟代表什么
- "std::unique_XXX"命名约定背后的基本原理是什么?
- 什么是 std::exception::what() 以及为什么要使用它?
- C++如果必须在编译时确定大小,std::array 有什么意义?
- 当我们进行一些操作时,应该使用什么'std::string'或'std::stringstream'?
- 如果 KEY 是 std::list 或 std::vector 而不是值,那么 std::map 的默认行为是什么?
- 当 std::move 与 C 样式数组或不移动对象时会发生什么
- 什么是自动 t1=std::make_tuple(case1==case2,整数)的值
- C++std::atomic在程序员级别保证了什么
- 当为可变性配置时,boost::heap::d_ary_heap 保留的额外 std::list 的目的是什么?
- 引用 std::any 或 not_yet_in_std::whatever 的惯用方式是什么?
- 在什么条件下使用 std::memcpy 在对象之间复制是安全的?
- 在自定义 std::vector-like 容器中处理指针和非指针模板类型的最佳方法是什么?
- 在 C# 中等效的 std::rotate() 是什么?
- 什么是 std::function::argument_type 的替代品?
- 什么是 std::jthread 在 c++20 中?
- std::enable_if 和 std::enable_if_t 有什么区别?
- "owned pointer"和 std::shared_ptr 的"stored pointer"有什么区别?