LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 1316|回复: 16

求助一个很菜的问题:gcc不能编译c++

[复制链接]
发表于 2004-4-9 17:38:01 | 显示全部楼层 |阅读模式
编译最简单的显示“你好”都不行,出错提示:
————————————————————————————
In file included from /usr/include/c++/3.2.2/backward/iostream.h:31,
                 from p7.cpp:1:
/usr/include/c++/3.2.2/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 <sstream> instead of the deprecated header <strstream.h>. To disable this warning use -Wno-deprecated.
p7.cpp:3: `main' must return `int'
————————————————————————————
请问这是为何,如何解决?
发表于 2004-4-9 18:31:31 | 显示全部楼层
g++
 楼主| 发表于 2004-4-12 09:44:35 | 显示全部楼层
不行呀,用
g++ p7.cpp
结果还是那个提示
发表于 2004-4-12 10:23:50 | 显示全部楼层
错误信息不是给的很清楚吗?
第一,你包含头文件,用的不是标准 C++ 的格式,例如包含 iostream,应该是 #include <iostream> 而不是 #include <iostream.h>

第二,你的 main 函数应该返回 int 类型的数据,最起码在最后加一个 return 0; 来表示顺利结束。

这两个错误都是 warning,程序应该可以正确执行。
 楼主| 发表于 2004-4-12 11:19:49 | 显示全部楼层
那怎么windows下的turbo c++ 3.0一切正常呢?
发表于 2004-4-12 12:09:58 | 显示全部楼层
最初由 朱镇东 发表
那怎么windows下的turbo c++ 3.0一切正常呢?

这些都是新的iso标准,turbo C++ 3.0使用的是以前的标准。
 楼主| 发表于 2004-4-12 14:52:00 | 显示全部楼层
哎,还是不行:
程序(清华大学钱能的《c++程序设计教程》上的第一个最简单的例子):
----------------------------------
#include <iostream>
void main()
{
cout << "I am a student.\n";
}
------------------------------------------
编译结果:
-----------------------------
p7.cpp:3: `main' must return `int'
p7.cpp: In function `int main(...)':
p7.cpp:4: `cout' undeclared (first use this function)
p7.cpp:4: (Each undeclared identifier is reported only once for each function it appears in.)
发表于 2004-4-12 15:01:41 | 显示全部楼层
改成这样试试:


  1. #include <iostream>
  2. using namespace std;

  3. int main()
  4. {
  5.   cout << "I am a student.\n";
  6.   return 0;
  7. }
复制代码


$ g++ -o iam iam.cpp
$ ./iam
I am a student.

从这个例子看,你该换本书。
发表于 2004-4-12 15:19:56 | 显示全部楼层

晕啊

原来学校教程这么落后啊 幸好 我没买那本书
发表于 2004-4-12 15:22:46 | 显示全部楼层
钱能的书我也看过,大都用的旧标准,但是gcc对旧标准是兼容的,应该可以通过。
你的程序没有引用标准名字空间,需要在include行下加一行“using namespace std;”
或者在“cout”前加“std::”
您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表