使用boost to_lower& amp;时出现gcc链接器错误修剪

时间:2009-07-24 05:12:35

标签: c++ gcc boost

我正在尝试在我的代码中使用boost库,但在Sparc Solaris平台下会遇到以下链接器错误。

问题代码基本上可归纳为:

#include <boost/algorithm/string.hpp>

std::string xparam;

... 
xparam = boost::to_lower(xparam);

链接器错误是:

LdapClient.cc:349: no match for `std::string& = void' operator
/opt/gcc-3.2.3/include/c++/3.2.3/bits/basic_string.h:338: candidates are: std::basic_string<_CharT, _Traits, _Alloc>& std::basic_string<_CharT, _Traits, _Alloc>::operator=(const std::basic_string<_CharT, _Traits, _Alloc>&) [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>]
/opt/gcc-3.2.3/include/c++/3.2.3/bits/basic_string.h:341:                 std::basic_string<_CharT, _Traits, _Alloc>& std::basic_string<_CharT, _Traits, _Alloc>::operator=(const _CharT*) [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>]
/opt/gcc-3.2.3/include/c++/3.2.3/bits/basic_string.h:344:                 std::basic_string<_CharT, _Traits, _Alloc>& std::basic_string<_CharT, _Traits, _Alloc>::operator=(_CharT) [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>]
gmake: *** [LdapClient.o] Error 1

有什么想法吗?

2 个答案:

答案 0 :(得分:4)

boost::to_lower不返回字符串的副本,它对传递给函数的变量进行操作。对于某些示例,read this

所以不需要重新分配:

boost::to_lower(xparam);

您将收到错误,因为您尝试将字符串分配给值void

如果要复制它,请使用复制版本:

std::string xparamLowered = boost::to_lower_copy(xparam);

答案 1 :(得分:2)

boost::to_lower modifies the string in-place,它不会返回新字符串。这就足够了:

boost::to_lower(xparam);

您的代码无法编译,因为to_lower的返回类型为void(如错误消息所示)。