#include<iostream> //不行,必须要加上.h
void main()
{
cout<<"Right?"<<endl;
}
#include<string>
#include<iostream.h> //不行,此处必须去掉.h
using namespace std ;
void main()
{
string s;
getline(cin,s);
cout<<"Right?"<<endl;
}
两者有什么区别?为什么有的时候要加上.h 而有的时候不能加???
关键在 using namespace std ;
iostream.h里面定义的所有类以及对象都是在全局空间里,所以你可以直接用cout
但在iostream里面,它所定义的东西都在名字空间std里面,所以你必须加上
using namespace std才能使用cout
看一下C++中的命名空间,因为STL库都是在std明明空间中实现的。
可以用std域修饰符限定:
#include <iostream.h>
#include <iostream>
void main()
{
cout << "hello" << endl ; // ok
std::cout << "hello" << std::endl ; // should be ok
}
#ifdef _RW_STD_IOSTREAM
#include <iostream>
#else
#include <iostream.h>
#endif
#ifndef _RWSTD_NO_NAMESPACE
using namespace std;
#endif