免费注册 查看新帖 |

Chinaunix

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

gcc下使用stl库出错,怎么解决呢?谢谢 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2004-11-02 17:45 |只看该作者 |倒序浏览
用了一个小程序测,还是不行

#include "iostream"
#include "string"

using namespace std;

enum TestType { one = 't', two, three };

template<typename CharType>;

class TestClass
  : public std::basic_string<CharType>;
{
        public:
          virtual void test(const CharType *test)
          {
                (*this).assign(test);
          }
};

int main(void)
{

  TestClass<TestType>; test;
  TestType t = one;
  test.test(&t);

  // cout << test << endl;
  return 0;
}

编译能通过,连接的时候出错,错误信息如下:

/tmp/ccXwDe9p.o(.text+0x9c): In function `__static_initialization_and_destruction_0(int, int)':
: undefined reference to `std::ios_base::Init::Init[in-charge]()'
/tmp/ccXwDe9p.o(.text+0xcb): In function `__tcf_0':
: undefined reference to `std::ios_base::Init::~Init [in-charge]()'
/tmp/ccXwDe9p.o(.gnu.linkonce.t._ZNSbI8TestTypeSt11char_traitsIS_ESaIS_EE6assignEPKS_+0x10): In function `std::basic_string<TestType, std::char_traits<TestType>;, std::allocator<TestType>; >;::assign(TestType const*)':
: undefined reference to `std::char_traits<TestType>;::length(TestType const*)'
/tmp/ccXwDe9p.o(.gnu.linkonce.t._ZNSbI8TestTypeSt11char_traitsIS_ESaIS_EE4_Rep10_M_destroyERKS2_+0x37): In function `std::basic_string<TestType, std::char_traits<TestType>;, std::allocator<TestType>; >;::_Rep::_M_destroy(std::allocator<TestType>; const&':
: undefined reference to `std::allocator<char>;::deallocate(char*, unsigned)'
/tmp/ccXwDe9p.o(.gnu.linkonce.t._ZNSbI8TestTypeSt11char_traitsIS_ESaIS_EE4_Rep10_M_destroyERKS2_+0x54): In function `std::basic_string<TestType, std::char_traits<TestType>;, std::allocator<TestType>; >;::_Rep::_M_destroy(std::allocator<TestType>; const&':
: undefined reference to `std::allocator<char>;::~allocator [in-charge]()'
/tmp/ccXwDe9p.o(.gnu.linkonce.t._ZNSbI8TestTypeSt11char_traitsIS_ESaIS_EE4_Rep10_M_destroyERKS2_+0x6b): In function `std::basic_string<TestType, std::char_traits<TestType>;, std::allocator<TestType>; >;::_Rep::_M_destroy(std::allocator<TestType>; const&':
: undefined reference to `std::allocator<char>;::~allocator [in-charge]()'
/tmp/ccXwDe9p.o(.gnu.linkonce.t._ZNSbI8TestTypeSt11char_traitsIS_ESaIS_EE4_Rep10_M_destroyERKS2_+0x8c): In function `std::basic_string<TestType, std::char_traits<TestType>;, std::allocator<TestType>; >;::_Rep::_M_destroy(std::allocator<TestType>; const&':
: undefined reference to `__cxa_call_unexpected'
/tmp/ccXwDe9p.o(.gnu.linkonce.t._ZNSbI8TestTypeSt11char_traitsIS_ESaIS_EE6assignEPKS_j+0x23): In function `std::basic_string<TestType, std::char_traits<TestType>;, std::allocator<TestType>; >;::assign(TestType const*, unsigned)':
: undefined reference to `std::__throw_length_error(char const*)'
/tmp/ccXwDe9p.o(.gnu.linkonce.t._ZNSbI8TestTypeSt11char_traitsIS_ESaIS_EE6assignEPKS_j+0x146): In function `std::basic_string<TestType, std::char_traits<TestType>;, std::allocator<TestType>; >;::assign(TestType const*, unsigned)':
: undefined reference to `std::char_traits<TestType>;::copy(TestType*, TestType const*, unsigned)'
/tmp/ccXwDe9p.o(.gnu.linkonce.t._ZNSbI8TestTypeSt11char_traitsIS_ESaIS_EE6assignEPKS_j+0x16b): In function `std::basic_string<TestType, std::char_traits<TestType>;, std::allocator<TestType>; >;::assign(TestType const*, unsigned)':
: undefined reference to `std::char_traits<TestType>;::move(TestType*, TestType const*, unsigned)'
/tmp/ccXwDe9p.o(.gnu.linkonce.t._ZNSbI8TestTypeSt11char_traitsIS_ESaIS_EE15_M_replace_safeIPKS_EERS3_N9__gnu_cxx17__normal_iteratorIPS_S3_EESB_T_SC_+0x52): In function `std::basic_string<TestType, std::char_traits<TestType>;, std::allocator<TestType>; >;& std::basic_string<TestType, std::char_traits<TestType>;, std::allocator<TestType>; >;::_M_replace_safe<TestType const*>;(__gnu_cxx::__normal_iterator<TestType*, std::basic_string<TestType, std::char_traits<TestType>;, std::allocator<TestType>; >; >;, __gnu_cxx::__normal_iterator<TestType*, std::basic_string<TestType, std::char_traits<TestType>;, std::allocator<TestType>; >; >;, TestType const*, __gnu_cxx::__normal_iterator<TestType*, std::basic_string<TestType, std::char_traits<TestType>;, std::allocator<TestType>; >; >':
: undefined reference to `std::__throw_length_error(char const*)'
/tmp/ccXwDe9p.o(.gnu.linkonce.t._ZNSbI8TestTypeSt11char_traitsIS_ESaIS_EE9_M_mutateEjjj+0x14d): In function `std::basic_string<TestType, std::char_traits<TestType>;, std::allocator<TestType>; >;::_M_mutate(unsigned, unsigned, unsigned)':
: undefined reference to `std::char_traits<TestType>;::copy(TestType*, TestType const*, unsigned)'
/tmp/ccXwDe9p.o(.gnu.linkonce.t._ZNSbI8TestTypeSt11char_traitsIS_ESaIS_EE9_M_mutateEjjj+0x184): In function `std::basic_string<TestType, std::char_traits<TestType>;, std::allocator<TestType>; >;::_M_mutate(unsigned, unsigned, unsigned)':
: undefined reference to `std::char_traits<TestType>;::copy(TestType*, TestType const*, unsigned)'
/tmp/ccXwDe9p.o(.gnu.linkonce.t._ZNSbI8TestTypeSt11char_traitsIS_ESaIS_EE9_M_mutateEjjj+0x197): In function `std::basic_string<TestType, std::char_traits<TestType>;, std::allocator<TestType>; >;::_M_mutate(unsigned, unsigned, unsigned)':
: undefined reference to `__cxa_begin_catch'
/tmp/ccXwDe9p.o(.gnu.linkonce.t._ZNSbI8TestTypeSt11char_traitsIS_ESaIS_EE9_M_mutateEjjj+0x1cf): In function `std::basic_string<TestType, std::char_traits<TestType>;, std::allocator<TestType>; >;::_M_mutate(unsigned, unsigned, unsigned)':
: undefined reference to `__cxa_rethrow'
/tmp/ccXwDe9p.o(.gnu.linkonce.t._ZNSbI8TestTypeSt11char_traitsIS_ESaIS_EE9_M_mutateEjjj+0x1da): In function `std::basic_string<TestType, std::char_traits<TestType>;, std::allocator<TestType>; >;::_M_mutate(unsigned, unsigned, unsigned)':
: undefined reference to `__cxa_end_catch'
/tmp/ccXwDe9p.o(.gnu.linkonce.t._ZNSbI8TestTypeSt11char_traitsIS_ESaIS_EE9_M_mutateEjjj+0x285): In function `std::basic_string<TestType, std::char_traits<TestType>;, std::allocator<TestType>; >;::_M_mutate(unsigned, unsigned, unsigned)':
: undefined reference to `std::char_traits<TestType>;::move(TestType*, TestType const*, unsigned)'
/tmp/ccXwDe9p.o(.gnu.linkonce.t._ZNSbI8TestTypeSt11char_traitsIS_ESaIS_EE13_S_copy_charsEPS_PKS_S6_+0x1c): In function `std::basic_string<TestType, std::char_traits<TestType>;, std::allocator<TestType>; >;::_S_copy_chars(TestType*, TestType const*, TestType const*)':
: undefined reference to `std::char_traits<TestType>;::copy(TestType*, TestType const*, unsigned)'
/tmp/ccXwDe9p.o(.gnu.linkonce.t._ZNSbI8TestTypeSt11char_traitsIS_ESaIS_EE4_Rep9_S_createEjRKS2_+0x19): In function `std::basic_string<TestType, std::char_traits<TestType>;, std::allocator<TestType>; >;::_Rep::_S_create(unsigned, std::allocator<TestType>; const&':
: undefined reference to `std::__throw_length_error(char const*)'
/tmp/ccXwDe9p.o(.gnu.linkonce.t._ZNSbI8TestTypeSt11char_traitsIS_ESaIS_EE4_Rep9_S_createEjRKS2_+0xd7): In function `std::basic_string<TestType, std::char_traits<TestType>;, std::allocator<TestType>; >;::_Rep::_S_create(unsigned, std::allocator<TestType>; const&':
: undefined reference to `std::allocator<char>;::allocate(unsigned, void const*)'
/tmp/ccXwDe9p.o(.gnu.linkonce.t._ZNSbI8TestTypeSt11char_traitsIS_ESaIS_EE4_Rep9_S_createEjRKS2_+0xf1): In function `std::basic_string<TestType, std::char_traits<TestType>;, std::allocator<TestType>; >;::_Rep::_S_create(unsigned, std::allocator<TestType>; const&':
: undefined reference to `std::allocator<char>;::~allocator [in-charge]()'
/tmp/ccXwDe9p.o(.gnu.linkonce.t._ZNSbI8TestTypeSt11char_traitsIS_ESaIS_EE4_Rep9_S_createEjRKS2_+0x10e): In function `std::basic_string<TestType, std::char_traits<TestType>;, std::allocator<TestType>; >;::_Rep::_S_create(unsigned, std::allocator<TestType>; const&':
: undefined reference to `std::allocator<char>;::~allocator [in-charge]()'
/tmp/ccXwDe9p.o(.gnu.linkonce.r._ZTI9TestClassI8TestTypeE+0x0): undefined reference to `vtable for __cxxabiv1::__vmi_class_type_info'
/tmp/ccXwDe9p.o(.gnu.linkonce.r._ZTISbI8TestTypeSt11char_traitsIS_ESaIS_EE+0x0): undefined reference to `vtable for __cxxabiv1::__class_type_info'
/tmp/ccXwDe9p.o(.eh_frame+0x12): undefined reference to `__gxx_personality_v0'
collect2: ld returned 1 exit status

论坛徽章:
0
2 [报告]
发表于 2004-11-02 17:49 |只看该作者

gcc下使用stl库出错,怎么解决呢?谢谢

GCC版本是:gcc (GCC) 3.3.2 20031022 (Red Hat Linux 3.3.2-1)
std库是:libstdc++.so.5.0.5

多谢啦

论坛徽章:
0
3 [报告]
发表于 2004-11-02 17:57 |只看该作者

gcc下使用stl库出错,怎么解决呢?谢谢

试试看用g++编译

论坛徽章:
0
4 [报告]
发表于 2004-11-02 18:08 |只看该作者

gcc下使用stl库出错,怎么解决呢?谢谢

也是同样的错:)

论坛徽章:
0
5 [报告]
发表于 2004-11-02 19:53 |只看该作者

gcc下使用stl库出错,怎么解决呢?谢谢

<iostream>;<string>;

论坛徽章:
0
6 [报告]
发表于 2004-11-04 14:43 |只看该作者

gcc下使用stl库出错,怎么解决呢?谢谢

改成<iostream>;<string>;还是同样的错...

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

gcc下使用stl库出错,怎么解决呢?谢谢

很简单楼主,你好好看看连接错误提示就知道了:

undefined reference to `std::char_traits<TestType>;::length(TestType const*)
undefined reference to `std::allocator<char>;::deallocate(char*, unsigned)'
undefined reference to `std::char_traits<TestType>;::move(TestType*, TestType const*, unsigned)'
undefined reference to `std::char_traits<TestType>;::copy(TestType*, TestType const*, unsigned)
undefined reference to `std::allocator<char>;::allocate(unsigned, void const*)'

STL里如果要使用预定义类型之外的其它类型实例化basic_string模板,就必须提供相应的char_traits和相应的赋值、复制和移动操作函数(关于traits的讨论太长了,这里我没办法说明,请参见侯捷侯先生的《STL源码剖析》)。你现在使用的是你定义的枚举类型作为CharType实例化basic_string,却又没有给出对应的char_traits和操作函数,那么连接时肯定会因为找不到函数定义而出错。

By the way,让自己的类从basic_string模板继承是一个非常傻的主意,因为basic_string模板设计出来就没有为继承考虑过。如果从它继承,则就不应该重载其中的任何成员(好在你没有重载)另外,根据assign()成员的语义,你那样写也是要出错的(内存非法越界)。

论坛徽章:
0
8 [报告]
发表于 2004-11-05 11:08 |只看该作者

gcc下使用stl库出错,怎么解决呢?谢谢

阿!!,好,多谢了:)我仔细看看书

论坛徽章:
1
荣誉会员
日期:2011-11-23 16:44:17
9 [报告]
发表于 2004-11-05 12:35 |只看该作者

gcc下使用stl库出错,怎么解决呢?谢谢

[quote="cppof286"]By the way,让自己的类从basic_string模板继承是一个非常傻的主意,因为basic_string模板设计出来就没有为继承考虑过。如果从它继承,则就不应该重载其中的任何成员(好在你没有重载)[quote]没有什么傻不傻的吧?我觉得看自己的应用而矣,如果真的需要自己的traits可以自己写就是了,只是要与标准的符合.

论坛徽章:
0
10 [报告]
发表于 2011-07-02 15:50 |只看该作者
加#include<vector>
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP