当前时区为 UTC + 8 小时



发表新帖 回复这个主题  [ 7 篇帖子 ] 
作者 内容
1 楼 
 文章标题 : 关于c++中使用string字符串变量碰到的问题
帖子发表于 : 2009-07-09 10:42 
头像

注册: 2006-03-04 23:10
帖子: 888
地址: 江西高安
送出感谢: 0 次
接收感谢: 2
例5.11 输入3个字符串,要求将字符按照由小到大的顺序输出。

代码:
//按照从小到大的顺序排列三个字符串

#include <iostream>
#include <string>
using namespace std;

int main()
{string string1,string2,string3,temp;
cout<<"please input three strings:";
cin>>string1>>string2>>string3;
if(string1>string2) temp=string1,string1=string2,string2=temp;
if(string2>string3) temp=string2,string2=string3,string3=temp;
if(string1>string2) temp=string1,string1=string2,string2=temp;
cout<<string1<<" "<<string2<<" "<<string3<<""<<endl;
return 0;
}


提示错误:
代码:
/tmp/cc2xtCcV.o: In function `__static_initialization_and_destruction_0(int, int)':
sortstr.cpp:(.text+0x1d): undefined reference to `std::ios_base::Init::Init()'
sortstr.cpp:(.text+0x22): undefined reference to `std::ios_base::Init::~Init()'
/tmp/cc2xtCcV.o: In function `main':
sortstr.cpp:(.text+0x76): undefined reference to `std::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string()'
sortstr.cpp:(.text+0x81): undefined reference to `std::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string()'
sortstr.cpp:(.text+0x8c): undefined reference to `std::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string()'
sortstr.cpp:(.text+0x97): undefined reference to `std::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string()'
sortstr.cpp:(.text+0xa6): undefined reference to `std::cout'
sortstr.cpp:(.text+0xab): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)'
sortstr.cpp:(.text+0xb9): undefined reference to `std::cin'
sortstr.cpp:(.text+0xbe): undefined reference to `std::basic_istream<char, std::char_traits<char> >& std::operator>><char, std::char_traits<char>, std::allocator<char> >(std::basic_istream<char, std::char_traits<char> >&, std::basic_string<char, std::char_traits<char>, std::allocator<char> >&)'
sortstr.cpp:(.text+0xcf): undefined reference to `std::basic_istream<char, std::char_traits<char> >& std::operator>><char, std::char_traits<char>, std::allocator<char> >(std::basic_istream<char, std::char_traits<char> >&, std::basic_string<char, std::char_traits<char>, std::allocator<char> >&)'
sortstr.cpp:(.text+0xe0): undefined reference to `std::basic_istream<char, std::char_traits<char> >& std::operator>><char, std::char_traits<char>, std::allocator<char> >(std::basic_istream<char, std::char_traits<char> >&, std::basic_string<char, std::char_traits<char>, std::allocator<char> >&)'
sortstr.cpp:(.text+0x108): undefined reference to `std::basic_string<char, std::char_traits<char>, std::allocator<char> >::operator=(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
sortstr.cpp:(.text+0x11a): undefined reference to `std::basic_string<char, std::char_traits<char>, std::allocator<char> >::operator=(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
sortstr.cpp:(.text+0x12c): undefined reference to `std::basic_string<char, std::char_traits<char>, std::allocator<char> >::operator=(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
sortstr.cpp:(.text+0x154): undefined reference to `std::basic_string<char, std::char_traits<char>, std::allocator<char> >::operator=(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
sortstr.cpp:(.text+0x166): undefined reference to `std::basic_string<char, std::char_traits<char>, std::allocator<char> >::operator=(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
/tmp/cc2xtCcV.o:sortstr.cpp:(.text+0x178): more undefined references to `std::basic_string<char, std::char_traits<char>, std::allocator<char> >::operator=(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)' follow
/tmp/cc2xtCcV.o: In function `main':
sortstr.cpp:(.text+0x1d2): undefined reference to `std::cout'
sortstr.cpp:(.text+0x1d7): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <char, std::char_traits<char>, std::allocator<char> >(std::basic_ostream<char, std::char_traits<char> >&, std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
sortstr.cpp:(.text+0x1e7): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)'
sortstr.cpp:(.text+0x1f8): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <char, std::char_traits<char>, std::allocator<char> >(std::basic_ostream<char, std::char_traits<char> >&, std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
sortstr.cpp:(.text+0x208): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)'
sortstr.cpp:(.text+0x219): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <char, std::char_traits<char>, std::allocator<char> >(std::basic_ostream<char, std::char_traits<char> >&, std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
sortstr.cpp:(.text+0x229): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)'
sortstr.cpp:(.text+0x231): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::endl<char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&)'
sortstr.cpp:(.text+0x239): undefined reference to `std::basic_ostream<char, std::char_traits<char> >::operator<<(std::basic_ostream<char, std::char_traits<char> >& (*)(std::basic_ostream<char, std::char_traits<char> >&))'
sortstr.cpp:(.text+0x24b): undefined reference to `std::basic_string<char, std::char_traits<char>, std::allocator<char> >::~basic_string()'
sortstr.cpp:(.text+0x264): undefined reference to `std::basic_string<char, std::char_traits<char>, std::allocator<char> >::~basic_string()'
sortstr.cpp:(.text+0x277): undefined reference to `std::basic_string<char, std::char_traits<char>, std::allocator<char> >::~basic_string()'
sortstr.cpp:(.text+0x290): undefined reference to `std::basic_string<char, std::char_traits<char>, std::allocator<char> >::~basic_string()'
sortstr.cpp:(.text+0x2a3): undefined reference to `std::basic_string<char, std::char_traits<char>, std::allocator<char> >::~basic_string()'
/tmp/cc2xtCcV.o:sortstr.cpp:(.text+0x2bc): more undefined references to `std::basic_string<char, std::char_traits<char>, std::allocator<char> >::~basic_string()' follow
/tmp/cc2xtCcV.o: In function `bool std::operator><char, std::char_traits<char>, std::allocator<char> >(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)':
sortstr.cpp:(.text._ZStgtIcSt11char_traitsIcESaIcEEbRKSbIT_T0_T1_ES8_[bool std::operator><char, std::char_traits<char>, std::allocator<char> >(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)]+0x14): undefined reference to `std::basic_string<char, std::char_traits<char>, std::allocator<char> >::compare(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&) const'
/tmp/cc2xtCcV.o:(.eh_frame+0x12): undefined reference to `__gxx_personality_v0'
collect2: ld 返回 1


_________________
QQ:273247606
EMAIL:yanghui1986527@gmail.com


页首
 用户资料  
 
2 楼 
 文章标题 : Re: 关于c++中使用string字符串变量碰到的问题
帖子发表于 : 2009-07-09 10:50 

注册: 2006-09-11 22:47
帖子: 2841
送出感谢: 0 次
接收感谢: 4
编译参数加 -lstdc++ 试试


页首
 用户资料  
 
3 楼 
 文章标题 : Re: 关于c++中使用string字符串变量碰到的问题
帖子发表于 : 2009-07-09 11:00 
头像

注册: 2006-03-04 23:10
帖子: 888
地址: 江西高安
送出感谢: 0 次
接收感谢: 2
编译通过。
我已经在头文件中引用了string文件,能否解释下为什么编译的时候要加上“-lstdc++ ”??
谢谢~

代码:
snowdream@snowdream:~/ubuntu/c$ gcc sortstr.cpp -lstdc++ -o sortstr
snowdream@snowdream:~/ubuntu/c$ ./sortstr
please input three strings:China Germany U.S.A
China Germany U.S.A


_________________
QQ:273247606
EMAIL:yanghui1986527@gmail.com


页首
 用户资料  
 
4 楼 
 文章标题 : Re: 关于c++中使用string字符串变量碰到的问题
帖子发表于 : 2009-07-09 11:00 

注册: 2009-07-09 10:57
帖子: 1
送出感谢: 0 次
接收感谢: 0 次
:em06


页首
 用户资料  
 
5 楼 
 文章标题 : Re: 关于c++中使用string字符串变量碰到的问题
帖子发表于 : 2009-07-09 11:01 
头像

注册: 2007-09-29 3:09
帖子: 5773
送出感谢: 0 次
接收感谢: 3
标准C++库的链接...


_________________
HUNT Unfortunately No Talent...


页首
 用户资料  
 
6 楼 
 文章标题 : Re: 关于c++中使用string字符串变量碰到的问题
帖子发表于 : 2009-07-09 11:02 
头像

注册: 2006-03-04 23:10
帖子: 888
地址: 江西高安
送出感谢: 0 次
接收感谢: 2
明白了,我一直都用g++来编译的,这次不小心用了gcc,才出现这个问题(缺少库文件)的。

代码:
snowdream@snowdream:~/ubuntu/c$ g++ sortstr.cpp -o sortstr
snowdream@snowdream:~/ubuntu/c$ ./sortstr
please input three strings:China Germany U.S.A
China Germany U.S.A


_________________
QQ:273247606
EMAIL:yanghui1986527@gmail.com


页首
 用户资料  
 
7 楼 
 文章标题 : Re: 关于c++中使用string字符串变量碰到的问题
帖子发表于 : 2009-07-09 11:43 
头像

注册: 2007-11-29 9:41
帖子: 8455
送出感谢: 0 次
接收感谢: 0 次
g++直接通过 ... 解决了就好


页首
 用户资料  
 
显示帖子 :  排序  
发表新帖 回复这个主题  [ 7 篇帖子 ] 

当前时区为 UTC + 8 小时


在线用户

正在浏览此版面的用户:没有注册用户 和 2 位游客


不能 在这个版面发表主题
不能 在这个版面回复主题
不能 在这个版面编辑帖子
不能 在这个版面删除帖子
不能 在这个版面提交附件

前往 :  
本站点为公益性站点,用于推广开源自由软件,由 DiaHosting VPSBudgetVM VPS 提供服务。
我们认为:软件应可免费取得,软件工具在各种语言环境下皆可使用,且不会有任何功能上的差异;
人们应有定制和修改软件的自由,且方式不受限制,只要他们自认为合适。

Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
简体中文语系由 王笑宇 翻译