关于VC6.0中的C++有关using语句的问题
事情是这个样子的,程序
#include <iostream>
#include <string>
#include <list>
//using namespace std;
using std::cout;
using std::endl;
using std::string;
using std::list;
int main()
{
list<int> iList;
for(int i = 0; i<10; i++)
{
iList.push_back(i);
}
for(list<int>::const_iterator citer = iList.begin();citer != iList.end(); citer++)
{
cout << *citer << "\t";
}
cout << endl;
return 0;
}
在dev-cpp(gcc)下编译通过,可是到了vc6.0下面,却出错,错误信息如下:
--------------------Configuration: vc_temp - Win32 Debug--------------------
Compiling...
main_vc.cpp
E:\WorkSpace\C++\Prj\vc_temp\main_vc.cpp(38) : error C2653: 'list<int,class std::allocator<int> >' : is not a class or namespace name
E:\WorkSpace\C++\Prj\vc_temp\main_vc.cpp(38) : error C2065: 'const_iterator' : undeclared identifier
E:\WorkSpace\C++\Prj\vc_temp\main_vc.cpp(38) : error C2146: syntax error : missing ';' before identifier 'citer'
E:\WorkSpace\C++\Prj\vc_temp\main_vc.cpp(38) : error C2065: 'citer' : undeclared identifier
E:\WorkSpace\C++\Prj\vc_temp\main_vc.cpp(38) : error C2679: binary '=' : no operator defined which takes a right-hand operand of type 'class std::list<int,class std::allocator<int> >::iterator' (or there is no acceptable conversion)
E:\WorkSpace\C++\Prj\vc_temp\main_vc.cpp(38) : error C2677: binary '!=' : no global operator defined which takes type 'class std::list<int,class std::allocator<int> >::iterator' (or there is no acceptable conversion)
E:\WorkSpace\C++\Prj\vc_temp\main_vc.cpp(38) : error C2146: syntax error : missing ')' before identifier 'citer'
E:\WorkSpace\C++\Prj\vc_temp\main_vc.cpp(38) : error C2059: syntax error : ';'
E:\WorkSpace\C++\Prj\vc_temp\main_vc.cpp(38) : error C2059: syntax error : ')'
E:\WorkSpace\C++\Prj\vc_temp\main_vc.cpp(39) : error C2143: syntax error : missing ';' before '{'
E:\WorkSpace\C++\Prj\vc_temp\main_vc.cpp(40) : error C2100: illegal indirection
Error executing xicl6.exe.
vc_temp.exe - 11 error(s), 0 warning(s)
后来试了试,如果在源文件开头添上using namespace std,vc能顺利编译运行!
小弟在这里问一下,为何?
因为在C++ Primer第四版中提到,要尽量少用using directive,而多用using declartion。
谢谢!