VC++中的一个问题

来源:百度知道 编辑:UC知道 时间:2024/05/17 03:55:55
#include "iostream"
void main()
{char a[10];
//cin>>a;
cin.get(a,10);
cout<<a<<endl;
}
////为什么必须加.h呢,不加就出错
//d:\c&c++\cin1.cpp(5) : error C2065: 'cin' : undeclared identifier
//d:\c&c++\cin1.cpp(5) : error C2228: left of '.get' must have class/struct/union type
//d:\c&c++\cin1.cpp(6) : error C2065: 'cout' : undeclared identifier
//d:\c&c++\cin1.cpp(6) : error C2297: '<<' : illegal, right operand has type 'char [10]'
//d:\c&c++\cin1.cpp(6) : error C2065: 'endl' : undeclared identifier

有两种方法

#include <iostream.h>

或者

#include <iostream>
using namespace std;

其实前者是不规范的,在C++里面,建议使用命令空间的做法,即后者。

用include就得加上扩展名

#include <iostream>

#include "iostream.h"
引入头文件,"iostream.h" 是引入的类的头文件名字
他是个文件名,".h"是后缀
如果你没写.h 头文件就引不进来,所以会有错误

如果用的VC++ 6.0的话我理解大概是这样的
比如你自定义一个类
MyClass
那么保存以后他的文件将有两个一个是"MyClass.h"这个是头文件,里面定义着类的成员,其中成员变量不能赋值,成员方法不能有具体实现,就相当于一个空壳子。具体类的方法的实现什么的都在"MyClass.cpp"里面写
而且"MyClass.cpp"里面将会有
#include "MyClass.h"

这句话写下后"MyClass.cpp"
文件里就可以写"MyClass.h"里面的方法的实现了
我理解就是重载一编那里面的方法

大概如此,如果有错误,请指正

不加 .h 编译器不知道"iostream"是什么东西