c++ - 我想从此代码中删除 using namespace std,但我不确定所有需要以 std::为前缀的内容

标签 c++ namespaces

我想删除 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;
}

最佳答案

只需删除它,然后在编译器出错的所有内容前加上前缀。 :P


快速浏览一下,肯定需要加前缀的是 coutendl
另外,请不要使用endl 来插入换行符,而是使用普通的'\n'endl 不仅会插入一个换行符,还会刷新流,如果经常使用(您确实如此),这可能会大大降低性能。仅当除了插入换行符之外还需要刷新流时才使用它。

关于c++ - 我想从此代码中删除 using namespace std,但我不确定所有需要以 std::为前缀的内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5609292/

相关文章:

c++ - 链接问题与 g++ 有关,但与 cc 无关

r - 如何在 R 包中正确使用其他包中的函数

xml - 使用带有命名空间的正确路径来解析 XML(在 VBA 中)

c++ - 我应该怎么做才能访问变量 'a'?

c++ - 确定列和行

c++ - 语法说明

database - 架构名称与表名称 - 如何避免冲突?

clojure - clojure/clojurescript 中单个 var 命名空间的命名约定是什么?

c++ - 在没有比较运算符的情况下查找 2 个数字之间的最小值

C++套接字编程: maximize throughput/bandwidth on localhost (I only get 3 Gbit/s instead of 23GBit/s)