[root@MagicLinux ~]# g++ -o test test.cpp
In file included from /usr/lib/gcc/i686-magic-linux/3.4.4/../../../../include/c++/3.4.4/backward/iostream.h:31,
from test.cpp:1:
/usr/lib/gcc/i686-magic-linux/3.4.4/../../../../include/c++/3.4.4/backward/backward_warning.h:32:2: warning: #warning This file includes at least one deprecated or antiquated header. Please consider using one of the 32 headers found in section 17.4.1.2 of the C++ standard. Examples include substituting the <X> header for the <X.h> header for C++ includes, or <iostream> instead of the deprecated header <iostream.h>. To disable this warning use -Wno-deprecated.
test.cpp:3: error: `main' must return `int'
test.cpp:5:2: warning: no newline at end of file
首先,#include <iostream> 和 int main 都 ISO C++ 要求的:
1. 因为在每个编译环境中(比如 Winodws 、UNIX)对定义 C++ 源程序的文件后缀是不一样的,有的定义为 .cpp,有的定义为 .c,因此 ISO C++ 把标准库的声明文件的后缀去掉了,以适应不同的环境。
在 gcc 中,保留了 .h 文件,但写 C++ 程序时,建议你使用 <iostream> 这种格式。
至于 using namespace std; ,你应该上网找一下什么是“名字空间”,<iostream> 里的 cout 是在一个叫 std 的名字空间中。要使用 using namespace 来定义,要不然你就要这样使用 cout 了:std::cout
首先,#include <iostream> 和 int main 都 ISO C++ 要求的:
1. 因为在每个编译环境中(比如 Winodws 、UNIX)对定义 C++ 源程序的文件后缀是不一样的,有的定义为 .cpp,有的定义为 .c,因此 ISO C++ 把标准库的声明文件的后缀去掉了,以适应不同的环境。
在 gcc 中,保留了 .h 文件,但写 C++ 程序时,建议你使用 <iostream> 这种格式。
至于 using namespace std; ,你应该上网找一下什么是“名字空间”,<iostream> 里的 cout 是在一个叫 std 的名字空间中。要使用 using namespace 来定义,要不然你就要这样使用 cout 了:std::cout
2. int main(),这也是 ISO C++ 的要求函数的类型不能为其它类型,只能是 int[/quote]
1、恩,谢谢板猪的提示哈,我一定好好的去看ISO标准,但似乎不声明using namespace std也可以用cout,只是用g++编译的时候,会出现大堆的提示,用了以后就清爽了。~~~~~~~~~不同标准害死人阿~~~~
2、原来是int main(),怪不得我写成 main()反而可以通过了。