免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2327 | 回复: 6
打印 上一主题 下一主题

[C++] 初学问题:redhat8.0 上gcc 编译c++程序的错误 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2003-05-04 12:12 |只看该作者 |倒序浏览
按照教材上的代码写了一个例子程序
/home/lyc/c++]more test1.cpp
#include <iostream>;
using namespace std
void read() { cout << "read()\n"; };
void sort() { cout << "sort()\n"; };
void compact() { cout << "compact()\n"; };
void write() { cout << "write()\n"; };
int main()
{
read();
sort();
compact();
write();
return 0;
}

可是编译出现好多错误,查了一些资料,好像没有好的
解决办法,也许因为这个太初级了吧,能帮忙看看吗?


/home/lyc/c++]gcc test1.cpp
test1.cpp:3: parse error before `void'
test1.cpp:4: `void' fails to be a typedef or built in type
test1.cpp:4: ISO C++ forbids declaration of `sort' with no type
test1.cpp: In function `int sort()':
test1.cpp:4: `cout' undeclared (first use this function)
test1.cpp:4: (Each undeclared identifier is reported only once for each
   function it appears in.)
test1.cpp: At global scope:
test1.cpp:5: `void' fails to be a typedef or built in type
test1.cpp:5: ISO C++ forbids declaration of `compact' with no type
test1.cpp:6: `void' fails to be a typedef or built in type
test1.cpp:6: ISO C++ forbids declaration of `write' with no type
test1.cpp: In function `int main()':
test1.cpp:9: `read' undeclared (first use this function)
test1.cpp:10: `sort' undeclared (first use this function)
test1.cpp:11: `compact' undeclared (first use this function)
test1.cpp:12: `write' undeclared (first use this function)
/usr/include/c++/3.2/bits/stl_algobase.h: In function `const _Tp&
   std::min(const _Tp&, const _Tp& [with _Tp = size_t]':
/usr/include/c++/3.2/bits/stl_algobase.h:793:   instantiated from here
/usr/include/c++/3.2/bits/stl_algobase.h:153: `__a' undeclared (first use this
   function)
/usr/include/c++/3.2/bits/stl_algobase.h:153: `__b' undeclared (first use this
   function)
/usr/include/c++/3.2/bits/stl_algobase.h: In function `bool
   std::lexicographical_compare(_InputIter1, _InputIter1, _InputIter2,
   _InputIter2) [with _InputIter1 = const signed char*, _InputIter2 = const
   signed char*]':
/usr/include/c++/3.2/bits/stl_algobase.h:805:   instantiated from here
/usr/include/c++/3.2/bits/stl_algobase.h:745: `__last2' undeclared (first use
   this function)
/usr/include/c++/3.2/bits/stl_algobase.h:745: `__first2' undeclared (first use
   this function)
/usr/include/c++/3.2/bits/stl_algobase.h:745: `__last1' undeclared (first use
   this function)
/usr/include/c++/3.2/bits/stl_algobase.h:745: `__first1' undeclared (first use
   this function)
/usr/include/c++/3.2/cmath: In function `_Tp std::__pow_helper(_Tp, int) [with
   _Tp = double]':
/usr/include/c++/3.2/cmath:419:   instantiated from here
/usr/include/c++/3.2/cmath:392: `__x' undeclared (first use this function)
/usr/include/c++/3.2/cmath:392: `__n' undeclared (first use this function)
/usr/include/c++/3.2/cmath:392: `__cmath_power' undeclared (first use this
   function)

然后修改代码为

/home/lyc/c++]more test1.cpp
#include <iostream>;
using namespace std ;
void read() { cout << "read()\n"; };
void sort() { cout << "sort()\n"; };
void compact() { cout << "compact()\n"; };
void write() { cout << "write()\n"; };
int main()
{
read();
sort();
compact();
write();
return 0;
}
加了一个分号,错误改变为

/home/lyc/c++]gcc test1.cpp
/tmp/ccjsGokZ.o: In function `read()':
/tmp/ccjsGokZ.o(.text+0xf): undefined reference to `std::cout'
/tmp/ccjsGokZ.o(.text+0x14): undefined reference to `std::basic_ostream<char, std::char_traits
<char>; >;& std:perator<< <std::char_traits<char>; >;(std::basic_ostream<char, std::char_traits<
char>; >;&, char const*)'
/tmp/ccjsGokZ.o: In function `sort()':
/tmp/ccjsGokZ.o(.text+0x2d): undefined reference to `std::cout'
/tmp/ccjsGokZ.o(.text+0x32): undefined reference to `std::basic_ostream<char, std::char_traits
<char>; >;& std:perator<< <std::char_traits<char>; >;(std::basic_ostream<char, std::char_traits<
char>; >;&, char const*)'
/tmp/ccjsGokZ.o: In function `compact()':
/tmp/ccjsGokZ.o(.text+0x4b): undefined reference to `std::cout'
/tmp/ccjsGokZ.o(.text+0x50): undefined reference to `std::basic_ostream<char, std::char_traits
<char>; >;& std:perator<< <std::char_traits<char>; >;(std::basic_ostream<char, std::char_traits<
char>; >;&, char const*)'
/tmp/ccjsGokZ.o: In function `write()':
/tmp/ccjsGokZ.o(.text+0x69): undefined reference to `std::cout'
/tmp/ccjsGokZ.o(.text+0x6e): undefined reference to `std::basic_ostream<char, std::char_traits
<char>; >;& std:perator<< <std::char_traits<char>; >;(std::basic_ostream<char, std::char_traits<
char>; >;&, char const*)'
/tmp/ccjsGokZ.o: In function `__static_initialization_and_destruction_0(int, int)':
/tmp/ccjsGokZ.o(.text+0xc2): undefined reference to `std::ios_base::Init::Init[in-charge]()'
/tmp/ccjsGokZ.o: In function `__tcf_0':
/tmp/ccjsGokZ.o(.text+0xf1): undefined reference to `std::ios_base::Init::~Init [in-charge]()'
/tmp/ccjsGokZ.o(.eh_frame+0x11): undefined reference to `__gxx_personality_v0'
collect2: ld returned 1 exit status

错在哪里呢?能否指点一二,谢谢

论坛徽章:
0
2 [报告]
发表于 2003-05-04 12:23 |只看该作者

初学问题:redhat8.0 上gcc 编译c++程序的错误

using namespace std
少了分号

另外错误提示都说得很清楚了
parse error before `void' 在第几行
出现分割符错误


你照错误检查一下自己的语法



还有就是写代码时使用论坛的code功能发帖(看发帖上有个CODE)

论坛徽章:
0
3 [报告]
发表于 2003-05-04 12:45 |只看该作者

初学问题:redhat8.0 上gcc 编译c++程序的错误

谢谢你的回答

  1. 加了一个分号,错误改变为

  2. /home/lyc/c++]gcc test1.cpp
  3. /tmp/ccjsGokZ.o: In function `read()':
  4. /tmp/ccjsGokZ.o(.text+0xf): undefined reference to `std::cout'
  5. /tmp/ccjsGokZ.o(.text+0x14): undefined reference to `std::basic_ostream<char, std::char_traits
  6. <char>; >;& std::operator<< <std::char_traits<char>; >;(std::basic_ostream<char, std::char_traits<
  7. char>; >;&, char const*)'
  8. /tmp/ccjsGokZ.o: In function `sort()':
  9. /tmp/ccjsGokZ.o(.text+0x2d): undefined reference to `std::cout'
  10. /tmp/ccjsGokZ.o(.text+0x32): undefined reference to `std::basic_ostream<char, std::char_traits
  11. <char>; >;& std::operator<< <std::char_traits<char>; >;(std::basic_ostream<char, std::char_traits<
  12. char>; >;&, char const*)'
  13. /tmp/ccjsGokZ.o: In function `compact()':
  14. /tmp/ccjsGokZ.o(.text+0x4b): undefined reference to `std::cout'
  15. /tmp/ccjsGokZ.o(.text+0x50): undefined reference to `std::basic_ostream<char, std::char_traits
  16. <char>; >;& std::operator<< <std::char_traits<char>; >;(std::basic_ostream<char, std::char_traits<
  17. char>; >;&, char const*)'
  18. /tmp/ccjsGokZ.o: In function `write()':
  19. /tmp/ccjsGokZ.o(.text+0x69): undefined reference to `std::cout'
  20. /tmp/ccjsGokZ.o(.text+0x6e): undefined reference to `std::basic_ostream<char, std::char_traits
  21. <char>; >;& std::operator<< <std::char_traits<char>; >;(std::basic_ostream<char, std::char_traits<
  22. char>; >;&, char const*)'
  23. /tmp/ccjsGokZ.o: In function `__static_initialization_and_destruction_0(int, int)':
  24. /tmp/ccjsGokZ.o(.text+0xc2): undefined reference to `std::ios_base::Init::Init[in-charge]()'
  25. /tmp/ccjsGokZ.o: In function `__tcf_0':
  26. /tmp/ccjsGokZ.o(.text+0xf1): undefined reference to `std::ios_base::Init::~Init [in-charge]()'
  27. /tmp/ccjsGokZ.o(.eh_frame+0x11): undefined reference to `__gxx_personality_v0'
  28. collect2: ld returned 1 exit status

  29. 错在哪里呢?能否指点一二,谢谢
复制代码

论坛徽章:
0
4 [报告]
发表于 2003-05-04 13:01 |只看该作者

初学问题:redhat8.0 上gcc 编译c++程序的错误

对cpp使用g++编译

另外说unrefrence表示连接出错
再看看是std::cout这些都是标准库的

所以是编译器使用错了

论坛徽章:
0
5 [报告]
发表于 2003-05-04 13:07 |只看该作者

初学问题:redhat8.0 上gcc 编译c++程序的错误

原帖由 "无双" 发表:
对cpp使用g++编译

另外说unrefrence表示连接出错
再看看是std::cout这些都是标准库的

所以是编译器使用错了

ok


这一次对了,非常感谢你,你真是一个好同志和
好版主
  1. /home/lyc/c++]g++ test1.cpp
  2. /home/lyc/c++]ll
  3. total 28
  4. -rwxrwxr-x    1 lyc      lyc         13273  5月  4 13:06 a.out
  5. -rw-rw-r--    1 lyc      lyc            71  5月  3 19:30 hello.c
  6. -rw-r--r--    1 lyc      lyc           273  5月  4 12:45 test1.cpp
  7. -rw-rw-r--    1 lyc      lyc            88  5月  3 19:55 test2.cpp
  8. /home/lyc/c++]./a.out
  9. read()
  10. sort()
  11. compact()
  12. write()
  13. /home/lyc/c++]
复制代码

论坛徽章:
0
6 [报告]
发表于 2003-05-04 13:15 |只看该作者

初学问题:redhat8.0 上gcc 编译c++程序的错误

你好好学
以后也会比现在的我好的

论坛徽章:
0
7 [报告]
发表于 2003-05-05 09:42 |只看该作者

初学问题:redhat8.0 上gcc 编译c++程序的错误

建议先看看斑主贴这篇入门级文章http://www.chinaunix.net/forum/viewtopic.php?t=58282,避免以后再犯低级错误

用c编译器编译c++程序,必错无疑嘛
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP