【问题标题】:Error in C++ eclipse project using Boost libraries使用 Boost 库的 C++ eclipse 项目中的错误
【发布时间】:2014-10-26 21:16:36
【问题描述】:

我在 Eclipse 中编译 C++ 项目时遇到了一些问题。 它运行良好,但是当我包含一个 boost 头文件(algorithm/string.hpp)时,它会显示这些错误:

16:25:53 **** Incremental Build of configuration Debug for project Emulator ****
make all 
Building file: ../src/Emulator.cpp
Invoking: GCC C++ Compiler
g++ -I/usr/lib/qt/include -I/root/boost/boost/ -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"src/Emulator.d" -MT"src/Emulator.d" -o "src/Emulator.o" "../src/Emulator.cpp"
In file included from /usr/include/boost/algorithm/string/finder.hpp:23:0,
                 from /usr/include/boost/algorithm/string/find.hpp:22,
                 from /usr/include/boost/algorithm/string/predicate.hpp:23,
                 from /root/boost/boost/algorithm/string.hpp:21,
                 from ../src/Emulator.cpp:12:
/usr/include/boost/algorithm/string/detail/finder.hpp: En la instanciación de ‘boost::iterator_range<Iterator> boost::algorithm::detail::token_finderF<PredicateT>::operator()(ForwardIteratorT, ForwardIteratorT) const [with ForwardIteratorT = __gnu_cxx::__normal_iterator<char*, std::basic_string<char> >; PredicateT = const char*]’:
/usr/include/boost/algorithm/string/concept.hpp:43:28:   se requiere desde ‘void boost::algorithm::FinderConcept<FinderT, IteratorT>::constraints() [with FinderT = boost::algorithm::detail::token_finderF<const char*>; IteratorT = __gnu_cxx::__normal_iterator<char*, std::basic_string<char> >]’
/usr/include/boost/concept/detail/has_constraints.hpp:32:62:   se requiere para la sustitución de ‘template<class Model> boost::concepts::detail::yes boost::concepts::detail::has_constraints_(Model*, boost::concepts::detail::wrap_constraints<Model, (& Model:: constraints)>*) [with Model = boost::algorithm::FinderConcept<boost::algorithm::detail::token_finderF<const char*>, __gnu_cxx::__normal_iterator<char*, std::basic_string<char> > >]’
/usr/include/boost/concept/detail/has_constraints.hpp:42:5:   se requiere desde ‘const bool boost::concepts::not_satisfied<boost::algorithm::FinderConcept<boost::algorithm::detail::token_finderF<const char*>, __gnu_cxx::__normal_iterator<char*, std::basic_string<char> > > >::value’
/usr/include/boost/concept/detail/has_constraints.hpp:45:31:   se requiere desde ‘struct boost::concepts::not_satisfied<boost::algorithm::FinderConcept<boost::algorithm::detail::token_finderF<const char*>, __gnu_cxx::__normal_iterator<char*, std::basic_string<char> > > >’
/usr/include/boost/mpl/if.hpp:67:11:   se requiere desde ‘struct boost::mpl::if_<boost::concepts::not_satisfied<boost::algorithm::FinderConcept<boost::algorithm::detail::token_finderF<const char*>, __gnu_cxx::__normal_iterator<char*, std::basic_string<char> > > >, boost::concepts::constraint<boost::algorithm::FinderConcept<boost::algorithm::detail::token_finderF<const char*>, __gnu_cxx::__normal_iterator<char*, std::basic_string<char> > > >, boost::concepts::requirement<boost::concepts::failed************ boost::algorithm::FinderConcept<boost::algorithm::detail::token_finderF<const char*>, __gnu_cxx::__normal_iterator<char*, std::basic_string<char> > >::************> >’
/usr/include/boost/concept/detail/general.hpp:50:8:   se requiere desde ‘struct boost::concepts::requirement_<void (*)(boost::algorithm::FinderConcept<boost::algorithm::detail::token_finderF<const char*>, __gnu_cxx::__normal_iterator<char*, std::basic_string<char> > >)>’
/usr/include/boost/algorithm/string/iter_find.hpp:77:13:   se requiere desde ‘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::token_finderF<const char*>]’
/usr/include/boost/algorithm/string/split.hpp:149:69:   se requiere desde ‘SequenceSequenceT& boost::algorithm::split(SequenceSequenceT&, RangeT&, PredicateT, boost::algorithm::token_compress_mode_type) [with SequenceSequenceT = std::vector<std::basic_string<char> >; RangeT = std::basic_string<char>; PredicateT = const char*]’
../src/Emulator.cpp:23:27:   se requiere desde aquí
/usr/include/boost/algorithm/string/detail/finder.hpp:583:45: error: expression cannot be used as a function
                             while( It2!=End && m_Pred(*It2) ) ++It2;
                                             ^
In file included from /usr/include/c++/4.8.3/algorithm:62:0,
                 from /usr/include/c++/4.8.3/ext/slist:47,
                 from /usr/include/boost/algorithm/string/std/slist_traits.hpp:16,
                 from /usr/include/boost/algorithm/string/std_containers_traits.hpp:23,
                 from /root/boost/boost/algorithm/string.hpp:18,
                 from ../src/Emulator.cpp:12:
/usr/include/c++/4.8.3/bits/stl_algo.h: En la instanciación de ‘_RandomAccessIterator std::__find_if(_RandomAccessIterator, _RandomAccessIterator, _Predicate, std::random_access_iterator_tag) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<char*, std::basic_string<char> >; _Predicate = const char*]’:
/usr/include/c++/4.8.3/bits/stl_algo.h:4465:41:   se requiere desde ‘_IIter std::find_if(_IIter, _IIter, _Predicate) [with _IIter = __gnu_cxx::__normal_iterator<char*, std::basic_string<char> >; _Predicate = const char*]’
/usr/include/boost/algorithm/string/detail/finder.hpp:570:74:   se requiere desde ‘boost::iterator_range<Iterator> boost::algorithm::detail::token_finderF<PredicateT>::operator()(ForwardIteratorT, ForwardIteratorT) const [with ForwardIteratorT = __gnu_cxx::__normal_iterator<char*, std::basic_string<char> >; PredicateT = const char*]’
/usr/include/boost/algorithm/string/concept.hpp:43:28:   se requiere desde ‘void boost::algorithm::FinderConcept<FinderT, IteratorT>::constraints() [with FinderT = boost::algorithm::detail::token_finderF<const char*>; IteratorT = __gnu_cxx::__normal_iterator<char*, std::basic_string<char> >]’
/usr/include/boost/concept/detail/has_constraints.hpp:32:62:   se requiere para la sustitución de ‘template<class Model> boost::concepts::detail::yes boost::concepts::detail::has_constraints_(Model*, boost::concepts::detail::wrap_constraints<Model, (& Model:: constraints)>*) [with Model = boost::algorithm::FinderConcept<boost::algorithm::detail::token_finderF<const char*>, __gnu_cxx::__normal_iterator<char*, std::basic_string<char> > >]’
/usr/include/boost/concept/detail/has_constraints.hpp:42:5:   se requiere desde ‘const bool boost::concepts::not_satisfied<boost::algorithm::FinderConcept<boost::algorithm::detail::token_finderF<const char*>, __gnu_cxx::__normal_iterator<char*, std::basic_string<char> > > >::value’
/usr/include/boost/concept/detail/has_constraints.hpp:45:31:   se requiere desde ‘struct boost::concepts::not_satisfied<boost::algorithm::FinderConcept<boost::algorithm::detail::token_finderF<const char*>, __gnu_cxx::__normal_iterator<char*, std::basic_string<char> > > >’
/usr/include/boost/mpl/if.hpp:67:11:   se requiere desde ‘struct boost::mpl::if_<boost::concepts::not_satisfied<boost::algorithm::FinderConcept<boost::algorithm::detail::token_finderF<const char*>, __gnu_cxx::__normal_iterator<char*, std::basic_string<char> > > >, boost::concepts::constraint<boost::algorithm::FinderConcept<boost::algorithm::detail::token_finderF<const char*>, __gnu_cxx::__normal_iterator<char*, std::basic_string<char> > > >, boost::concepts::requirement<boost::concepts::failed************ boost::algorithm::FinderConcept<boost::algorithm::detail::token_finderF<const char*>, __gnu_cxx::__normal_iterator<char*, std::basic_string<char> > >::************> >’
/usr/include/boost/concept/detail/general.hpp:50:8:   se requiere desde ‘struct boost::concepts::requirement_<void (*)(boost::algorithm::FinderConcept<boost::algorithm::detail::token_finderF<const char*>, __gnu_cxx::__normal_iterator<char*, std::basic_string<char> > >)>’
/usr/include/boost/algorithm/string/iter_find.hpp:77:13:   se requiere desde ‘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::token_finderF<const char*>]’
/usr/include/boost/algorithm/string/split.hpp:149:69:   se requiere desde ‘SequenceSequenceT& boost::algorithm::split(SequenceSequenceT&, RangeT&, PredicateT, boost::algorithm::token_compress_mode_type) [with SequenceSequenceT = std::vector<std::basic_string<char> >; RangeT = std::basic_string<char>; PredicateT = const char*]’
../src/Emulator.cpp:23:27:   se requiere desde aquí
/usr/include/c++/4.8.3/bits/stl_algo.h:214:23: error: ‘__pred’ cannot be used as a function
    if (__pred(*__first))
                       ^
/usr/include/c++/4.8.3/bits/stl_algo.h:218:23: error: ‘__pred’ cannot be used as a function
    if (__pred(*__first))
                       ^
/usr/include/c++/4.8.3/bits/stl_algo.h:222:23: error: ‘__pred’ cannot be used as a function
    if (__pred(*__first))
                       ^
/usr/include/c++/4.8.3/bits/stl_algo.h:226:23: error: ‘__pred’ cannot be used as a function
    if (__pred(*__first))
                       ^
/usr/include/c++/4.8.3/bits/stl_algo.h:234:23: error: ‘__pred’ cannot be used as a function
    if (__pred(*__first))
                       ^
/usr/include/c++/4.8.3/bits/stl_algo.h:238:23: error: ‘__pred’ cannot be used as a function
    if (__pred(*__first))
                       ^
/usr/include/c++/4.8.3/bits/stl_algo.h:242:23: error: ‘__pred’ cannot be used as a function
    if (__pred(*__first))
                       ^
make: *** [src/Emulator.o] Error 1

16:26:04 Build Finished (took 11s.425ms)

当我使用另一个库(iostream、vector、string、fstream...)时,它说 Eclipse 找不到那个库,但是如果我在没有 boost 标头的情况下编译它,它就可以工作。 这是我的代码:

//============================================================================
// Name        : Emulator.cpp
// Author      : Manulaiko
// Version     : 1.0
// Description : SpaceBattles' emulator main file
//============================================================================

#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include "/root/boost/boost/algorithm/string.hpp"

using namespace std;

void loadConfig()
{
    ifstream file("settings");
    string s;

    while(getline(file, s)) {
        vector<string> line;
        boost::split(line, s,"=");

        //Test
        cout << line[0] << " = " << line[1] << endl;
    }
}

void connect()
{

}

int main() {
    cout << "SpaceBattles C++ emulator 1.0 by Manulaiko" << endl;
    cout << endl;

    cout << "Reading settings file..." << endl;
    loadConfig();

    cout << "Establishing connection to MySQL..." << endl;
    connect();
}

【问题讨论】:

  • 不知道 GCC 的本地化版本存在。
  • 您查看过boost::split 的文档或示例吗?
  • @πάνταῥεῖ 我现在看到了。情况更糟;)

标签: c++ eclipse boost project


【解决方案1】:

boost::split() 的第三个参数必须是函数(或“可调用”),而不是字符串。试试

boost::split(line, s, boost::is_any_of("="));

is_any_of()其实是一个构造函数,只不过它构造的是一个对象,可以像函数一样被调用。

可能有比is_any_of 更好的东西(例如is_equal),但我直接从文档中提取了它:http://www.boost.org/doc/libs/1_56_0/doc/html/string_algo/usage.html#idp432343152

【讨论】:

    猜你喜欢
    • 2023-03-24
    • 1970-01-01
    • 2017-03-23
    • 1970-01-01
    • 2011-08-26
    • 2011-05-25
    • 1970-01-01
    • 2012-10-25
    • 1970-01-01
    相关资源
    最近更新 更多