直接上错误代码实例

#include <iostream.h>

int main()
{
	print('hello, world\n')
	return 0;
 } 

编译通不过,直接出错

[Error] iostream.h: No such file or directory

这是C语言转C++的两条经典错误

  1. C++中是没有iostream.h这个东西的(或者一般不会这么使用),正确用法是:

    # include <iostream>

  2. 用了iostream还不能直接使用cincout,还需要添加命名空间

    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
请按任意键继续. . .

Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐