【问题标题】:Can't compile this Boost Algorithm example on GCC无法在 GCC 上编译此 Boost 算法示例
【发布时间】:2011-12-30 02:25:05
【问题描述】:
#include <boost/algorithm/string.hpp>
#include <vector>
#include <iostream>
#include <string>

using namespace std;
using namespace boost;

int main(int, char **)
{
    string test = "h:help";

    vector<string> v;

    iter_split(v, test, first_finder("h:"));

    copy(v.begin(), v.end(), ostream_iterator<string>(cout, "\n"));
    return 0;
}

这个简单的代码在使用-std=c++0x的GCC 4.6中不能编译;但是,它可以在较旧的编译器 (4.2) 和没有 C++0x 模式的情况下正常编译。

错误信息输出为:

In file included from /usr/include/boost/algorithm/string/split.hpp:15:0,
                 from /usr/include/boost/algorithm/string.hpp:22,
                 from test.cpp:1:
/usr/include/boost/algorithm/string/iter_find.hpp: In function 'SequenceSequenceT& boost::algorithm::iter_split(SequenceSequenceT&, RangeT&, FinderT) [with SequenceSequenceT = std::vector<std::basic_string<char> >, RangeT = std::basic_string<char>, FinderT = boost::algorithm::detail::first_finderF<const char*, boost::algorithm::is_equal>]':
test.cpp:15:47:   instantiated from here
/usr/include/boost/algorithm/string/iter_find.hpp:154:51: error: call of overloaded 'end(std::basic_string<char>&)' is ambiguous
/usr/include/boost/algorithm/string/iter_find.hpp:154:51: note: candidates are:
/usr/include/boost/range/end.hpp:145:56: note: typename boost::range_iterator<typename boost::remove_const<T>::type>::type boost::end(T&) [with T = std::basic_string<char>, typename boost::range_iterator<typename boost::remove_const<T>::type>::type = __gnu_cxx::__normal_iterator<char*, std::basic_string<char> >]
/usr/include/boost/range/end.hpp:156:61: note: typename boost::range_const_iterator<C>::type boost::end(const T&) [with T = std::basic_string<char>, typename boost::range_const_iterator<C>::type = __gnu_cxx::__normal_iterator<const char*, std::basic_string<char> >]
/usr/lib/gcc/i686-linux-gnu/4.6.2/../../../../include/c++/4.6.2/bits/range_access.h:78:5: note: decltype (__cont.end()) std::end(const _Container&) [with _Container = std::basic_string<char>, decltype (__cont.end()) = __gnu_cxx::__normal_iterator<const char*, std::basic_string<char> >]
/usr/lib/gcc/i686-linux-gnu/4.6.2/../../../../include/c++/4.6.2/bits/range_access.h:68:5: note: decltype (__cont.end()) std::end(_Container&) [with _Container = std::basic_string<char>, decltype (__cont.end()) = __gnu_cxx::__normal_iterator<char*, std::basic_string<char> >]
test.cpp:15:47:   instantiated from here
/usr/include/boost/algorithm/string/iter_find.hpp:162:39: error: call of overloaded 'begin(std::basic_string<char>&)' is ambiguous
/usr/include/boost/algorithm/string/iter_find.hpp:162:39: note: candidates are:
/usr/include/boost/range/begin.hpp:146:64: note: typename boost::range_iterator<typename boost::remove_const<T>::type>::type boost::begin(T&) [with T = std::basic_string<char>, typename boost::range_iterator<typename boost::remove_const<T>::type>::type = __gnu_cxx::__normal_iterator<char*, std::basic_string<char> >]
/usr/include/boost/range/begin.hpp:157:61: note: typename boost::range_const_iterator<C>::type boost::begin(const T&) [with T = std::basic_string<char>, typename boost::range_const_iterator<C>::type = __gnu_cxx::__normal_iterator<const char*, std::basic_string<char> >]
/usr/lib/gcc/i686-linux-gnu/4.6.2/../../../../include/c++/4.6.2/bits/range_access.h:58:5: note: decltype (__cont.begin()) std::begin(const _Container&) [with _Container = std::basic_string<char>, decltype (__cont.begin()) = __gnu_cxx::__normal_iterator<const char*, std::basic_string<char> >]
/usr/lib/gcc/i686-linux-gnu/4.6.2/../../../../include/c++/4.6.2/bits/range_access.h:48:5: note: decltype (__cont.begin()) std::begin(_Container&) [with _Container = std::basic_string<char>, decltype (__cont.begin()) = __gnu_cxx::__normal_iterator<char*, std::basic_string<char> >]

这可能是 GCC 中的错误吗?或者我可以对代码做些什么来解决这个问题?

【问题讨论】:

  • 使用 MinGW (gcc 4.5.0) 为我工作。
  • 什么版本的Boost?它更可能是旧版本 Boost 中的错误而不是编译器错误......
  • 哦,我没想到。 BOOST_LIB_VERSION 出现在 1_34_1。
  • Boost 1.34.1 已经快 4.5 年了;显然,当时 C++11 的支持/兼容性并不是一个问题。 ;-] Boost 1.48.0 是最新的。

标签: c++ gcc boost c++11 g++


【解决方案1】:

这只是您使用的超过 4 年的 Boost 版本中的一个错误。升级到最新版本(1.48.0 是最新的),您的代码将编译干净。

【讨论】:

    【解决方案2】:

    确保你已经安装了 boost-devel 包

    在 Centos 中以 root 身份执行:

    yum install boost-devel
    

    然后

    root@centos6 /]# find / -name boost
    /usr/include/boost
    /usr/lib64/boost
    [root@centos6 /]# 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-07-23
      • 2018-03-12
      • 1970-01-01
      • 2017-03-26
      • 1970-01-01
      • 1970-01-01
      • 2011-10-16
      • 2017-11-18
      相关资源
      最近更新 更多