不要随便使用 using namespace std

今天写了这么个小程序报错,报错 :count不明确

#include "stdafx.h"
#include<iostream>
using namespace std;

int count = 0;

int main()
{
    for (int i = 0; i < 10; i++)
    {
        static  int number = 0;
        for (int j = i; j < 10; j++)
        {
            number++;
            cout << number << endl;
            count++;
        }


    }


    cout << count << endl;
}

后来发现定义的全局变量count与std里面已有的名字重复,所以为了避免这个,平时多用
using std::这样写

#include "stdafx.h"
#include<iostream>
using  std::cout;
using std::endl;

int count = 0;

int main()
{
    for (int i = 0; i < 10; i++)
    {
        static  int number = 0;
        for (int j = i; j < 10; j++)
        {
            number++;
            cout << number << endl;
            count++;
        }


    }


    cout << count << endl;
}

猜你喜欢

转载自blog.csdn.net/alexhu2010q/article/details/82023702