无法在 GCC 上编译此 Boost 算法示例

发布于 2024-12-15 15:50:57 字数 4236 浏览 2 评论 0原文

#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;
}

这个简单的代码无法在 GCC 4.6 中使用 -std=c++0x 进行编译;但是,它可以在较旧的编译器 (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> >]

Could this be a bug in GCC?或者我可以对代码做些什么来解决这个问题?

#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;
}

This simple code will not compile in GCC 4.6 using -std=c++0x; it will, however, compile fine in both an older compiler (4.2) and without the C++0x mode.

The error message output is:

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> >]

Could this be a bug in GCC? Or is there something I could do to the code to fix this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

请别遗忘我 2024-12-22 15:50:57

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

This is merely a bug in the over-4-years-old version of Boost you're using. Upgrade to something more recent (1.48.0 is current) and your code will compile cleanly.

月亮是我掰弯的 2024-12-22 15:50:57

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

在 Centos 中以 root 身份执行:

yum install boost-devel

然后

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

Ensure that you have installed boost-devel package

In Centos execute as root:

yum install boost-devel

then

root@centos6 /]# find / -name boost
/usr/include/boost
/usr/lib64/boost
[root@centos6 /]# 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文