直接上错误代码实例
#include <iostream.h>
int main()
{
print('hello, world\n')
return 0;
}
编译通不过,直接出错
[Error] iostream.h: No such file or directory
这是C语言转C++的两条经典错误
-
C++中是没有
iostream.h这个东西的(或者一般不会这么使用),正确用法是:# include <iostream> -
用了
iostream还不能直接使用cin和cout,还需要添加命名空间:using namespace std;
正确代码实例
#include <iostream>
#include <string.h> // memset在string.h这个库里面
using namespace std;
int main()
{
int num[8];
memset(num, 1, 32);
for (int i=0; i<8; i++)
{
cout << num[i] << ' ';
}
return 0;
}
运行效果:
16843009 16843009 16843009 16843009 16843009 16843009 16843009 16843009
--------------------------------
Process exited after 0.2827 seconds with return value 0
请按任意键继续. . .
本文通过实例解析了从C语言转向C++时常见的编译错误,如使用错误的头文件iostream.h,并展示了正确的C++代码编写方式,包括如何正确引入标准输入输出流和使用内存操作函数。
410

被折叠的 条评论
为什么被折叠?



