为什么Microsoft Visual C++ 6.0能编译C语言代码,而不能编译C++代码。

如:
#include"iostream.h"

int add(int x,int y)
{
int z;
z=x+y;
return z;
}
void main()
{
int a,b,c;

cin>>a>>b;
c=add(a,b);
cout<<c<<endl;
}
而编译时报错!
--------------------Configuration: cp_1 - Win32 Debug--------------------
Compiling...
test.c
F:\VC 6\MyProjects\cp_1\test.c(13) : error C2065: 'cin' : undeclared identifier
F:\VC 6\MyProjects\cp_1\test.c(13) : warning C4552: '>>' : operator has no effect; expected operator with side-effect
F:\VC 6\MyProjects\cp_1\test.c(15) : error C2065: 'cout' : undeclared identifier
F:\VC 6\MyProjects\cp_1\test.c(15) : error C2065: 'endl' : undeclared identifier
F:\VC 6\MyProjects\cp_1\test.c(15) : warning C4552: '<<' : operator has no effect; expected operator with side-effect
Error executing cl.exe.

test.obj - 3 error(s), 2 warning(s)

首先,你要将建立cpp文件,不要建立c文件,前者是c++ 后者是C。你修改一下试试,
温馨提示:内容为网友见解,仅供参考
第1个回答  2011-10-29
因为你的头文件iostream.h是C的输入输出流头文件 而非C++的
但是cout << cin >> endl 等是C++特有的 所以用C的头文件自然不行
头文件需要改成<iostream> 并且在下一行加上using namespace std;就可以了
下附正确程序:
#include<iostream>
using namespace std;
int add(int x,int y)
{
int z;
z=x+y;
return z;
}
int main()
{
int a,b,c;
cin>>a>>b;
c=add(a,b);
cout<<c<<endl;
return 0;
}
第2个回答  2011-10-29
明显是语法错误
第3个回答  2011-10-29
在那些符号前面全部加上std::试试
第4个回答  2011-10-29
你的软件缺少了文件
相似回答
大家正在搜