开发者

Boost errors 'assignment of read-only location' and 'no matching function for call' (new install)

I'm just trying out the Boost libraries and have run into an error.

The code is simply:

#include <string>
#include <iostream>  
#include "/usr/include/boost/algorithm/string.hpp";
#include "/usr/include/boost/algorithm/string/erase.hpp";

using namespace std;
using namespace boost;
using namespace boost::algorithm;
using namespace boost::string;


int main()
{
    erase_all(" ","the quick brown fox");
    return 1;
}

When I compile it I get a massive block of text of which the following two lines I think are useful:

/usr/include/boost/algorithm/string/detail/find_format_all.hpp:249: error: no matching function for call to âerase(const char [2], const char*&, const char*)â

and

/usr/include/boost/algorithm/string/detail/replace_storage.hpp:98: error: assignment of read-only location

I've tried a simple Boost example from the Boost website and that works, but despite alot of fiddling and searching I cannot work out why this erase_all function will not work. I have a feeling it's a missing include but I can't work out which.

Can anyone shed any light on this?

The full error output is:

test1.cpp:3:51: warning: extra tokens at end of #include directive
/usr/include/boost/algorithm/string/detail/find_format_all.hpp: In function âvoid boost::algorithm::开发者_运维技巧detail::find_format_all_impl2(InputT&, FinderT, FormatterT, FindResultT, FormatResultT) [with InputT = const char [2], FinderT = boost::algorithm::detail::first_finderF<const char*, boost::algorithm::is_equal>, FormatterT = boost::algorithm::detail::empty_formatF<char>, FindResultT = boost::iterator_range<const char*>, FormatResultT = boost::algorithm::detail::empty_container<char>]â:
/usr/include/boost/algorithm/string/detail/find_format_all.hpp:179:   instantiated from âvoid boost::algorithm::detail::find_format_all_impl(InputT&, FinderT, FormatterT, FindResultT) [with InputT = const char [2], FinderT = boost::algorithm::detail::first_finderF<const char*, boost::algorithm::is_equal>, FormatterT = boost::algorithm::detail::empty_formatF<char>, FindResultT = boost::iterator_range<const char*>]â
/usr/include/boost/algorithm/string/find_format.hpp:247:   instantiated from âvoid boost::algorithm::find_format_all(SequenceT&, FinderT, FormatterT) [with SequenceT = const char [2], FinderT = boost::algorithm::detail::first_finderF<const char*, boost::algorithm::is_equal>, FormatterT = boost::algorithm::detail::empty_formatF<char>]â
/usr/include/boost/algorithm/string/erase.hpp:588:   instantiated from âvoid boost::algorithm::erase_all(SequenceT&, const RangeT&) [with SequenceT = const char [2], RangeT = char [20]]â
test1.cpp:12:   instantiated from here
/usr/include/boost/algorithm/string/detail/find_format_all.hpp:249: error: no matching function for call to âerase(const char [2], const char*&, const char*)â
/usr/include/boost/algorithm/string/detail/find_format_all.hpp:179:   instantiated from âvoid boost::algorithm::detail::find_format_all_impl(InputT&, FinderT, FormatterT, FindResultT) [with InputT = const char [2], FinderT = boost::algorithm::detail::first_finderF<const char*, boost::algorithm::is_equal>, FormatterT = boost::algorithm::detail::empty_formatF<char>, FindResultT = boost::iterator_range<const char*>]â
/usr/include/boost/algorithm/string/find_format.hpp:247:   instantiated from âvoid boost::algorithm::find_format_all(SequenceT&, FinderT, FormatterT) [with SequenceT = const char [2], FinderT = boost::algorithm::detail::first_finderF<const char*, boost::algorithm::is_equal>, FormatterT = boost::algorithm::detail::empty_formatF<char>]â
/usr/include/boost/algorithm/string/erase.hpp:588:   instantiated from âvoid boost::algorithm::erase_all(SequenceT&, const RangeT&) [with SequenceT = const char [2], RangeT = char [20]]â
test1.cpp:12:   instantiated from here
/usr/include/boost/algorithm/string/detail/find_format_all.hpp:254: error: no matching function for call to âinsert(const char [2], const char*, std::_Deque_iterator<char, char&, char*>, std::_Deque_iterator<char, char&, char*>)â
/usr/include/boost/algorithm/string/detail/replace_storage.hpp: In member function âForwardIteratorT boost::algorithm::detail::process_segment_helper<HasStableIterators>::operator()(StorageT&, InputT&, ForwardIteratorT, ForwardIteratorT, ForwardIteratorT) [with StorageT = std::deque<char, std::allocator<char> >, InputT = const char [2], ForwardIteratorT = const char*, bool HasStableIterators = false]â:
/usr/include/boost/algorithm/string/detail/replace_storage.hpp:150:   instantiated from âForwardIteratorT boost::algorithm::detail::process_segment(StorageT&, InputT&, ForwardIteratorT, ForwardIteratorT, ForwardIteratorT) [with StorageT = std::deque<char, std::allocator<char> >, InputT = const char [2], ForwardIteratorT = const char*]â
/usr/include/boost/algorithm/string/detail/find_format_all.hpp:221:   instantiated from âvoid boost::algorithm::detail::find_format_all_impl2(InputT&, FinderT, FormatterT, FindResultT, FormatResultT) [with InputT = const char [2], FinderT = boost::algorithm::detail::first_finderF<const char*, boost::algorithm::is_equal>, FormatterT = boost::algorithm::detail::empty_formatF<char>, FindResultT = boost::iterator_range<const char*>, FormatResultT = boost::algorithm::detail::empty_container<char>]â
/usr/include/boost/algorithm/string/detail/find_format_all.hpp:179:   instantiated from âvoid boost::algorithm::detail::find_format_all_impl(InputT&, FinderT, FormatterT, FindResultT) [with InputT = const char [2], FinderT = boost::algorithm::detail::first_finderF<const char*, boost::algorithm::is_equal>, FormatterT = boost::algorithm::detail::empty_formatF<char>, FindResultT = boost::iterator_range<const char*>]â
/usr/include/boost/algorithm/string/find_format.hpp:247:   instantiated from âvoid boost::algorithm::find_format_all(SequenceT&, FinderT, FormatterT) [with SequenceT = const char [2], FinderT = boost::algorithm::detail::first_finderF<const char*, boost::algorithm::is_equal>, FormatterT = boost::algorithm::detail::empty_formatF<char>]â
/usr/include/boost/algorithm/string/erase.hpp:588:   instantiated from âvoid boost::algorithm::erase_all(SequenceT&, const RangeT&) [with SequenceT = const char [2], RangeT = char [20]]â
test1.cpp:12:   instantiated from here
/usr/include/boost/algorithm/string/detail/replace_storage.hpp:98: error: assignment of read-only location
/usr/include/boost/algorithm/string/detail/replace_storage.hpp: In function âOutputIteratorT boost::algorithm::detail::move_from_storage(StorageT&, OutputIteratorT, OutputIteratorT) [with StorageT = std::deque<char, std::allocator<char> >, OutputIteratorT = const char*]â:
/usr/include/boost/algorithm/string/detail/replace_storage.hpp:70:   instantiated from âForwardIteratorT boost::algorithm::detail::process_segment_helper<HasStableIterators>::operator()(StorageT&, InputT&, ForwardIteratorT, ForwardIteratorT, ForwardIteratorT) [with StorageT = std::deque<char, std::allocator<char> >, InputT = const char [2], ForwardIteratorT = const char*, bool HasStableIterators = false]â
/usr/include/boost/algorithm/string/detail/replace_storage.hpp:150:   instantiated from âForwardIteratorT boost::algorithm::detail::process_segment(StorageT&, InputT&, ForwardIteratorT, ForwardIteratorT, ForwardIteratorT) [with StorageT = std::deque<char, std::allocator<char> >, InputT = const char [2], ForwardIteratorT = const char*]â
/usr/include/boost/algorithm/string/detail/find_format_all.hpp:221:   instantiated from âvoid boost::algorithm::detail::find_format_all_impl2(InputT&, FinderT, FormatterT, FindResultT, FormatResultT) [with InputT = const char [2], FinderT = boost::algorithm::detail::first_finderF<const char*, boost::algorithm::is_equal>, FormatterT = boost::algorithm::detail::empty_formatF<char>, FindResultT = boost::iterator_range<const char*>, FormatResultT = boost::algorithm::detail::empty_container<char>]â
/usr/include/boost/algorithm/string/detail/find_format_all.hpp:179:   instantiated from âvoid boost::algorithm::detail::find_format_all_impl(InputT&, FinderT, FormatterT, FindResultT) [with InputT = const char [2], FinderT = boost::algorithm::detail::first_finderF<const char*, boost::algorithm::is_equal>, FormatterT = boost::algorithm::detail::empty_formatF<char>, FindResultT = boost::iterator_range<const char*>]â
/usr/include/boost/algorithm/string/find_format.hpp:247:   instantiated from âvoid boost::algorithm::find_format_all(SequenceT&, FinderT, FormatterT) [with SequenceT = const char [2], FinderT = boost::algorithm::detail::first_finderF<const char*, boost::algorithm::is_equal>, FormatterT = boost::algorithm::detail::empty_formatF<char>]â
/usr/include/boost/algorithm/string/erase.hpp:588:   instantiated from âvoid boost::algorithm::erase_all(SequenceT&, const RangeT&) [with SequenceT = const char [2], RangeT = char [20]]â
test1.cpp:12:   instantiated from here
/usr/include/boost/algorithm/string/detail/replace_storage.hpp:35: error: assignment of read-only location
/usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../include/c++/4.1.2/bits/stl_algobase.h: In function â_OI std::__copy_aux(_II, _II, _OI) [with _II = const char*, _OI = const char*]â:
/usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../include/c++/4.1.2/bits/stl_algobase.h:326:   instantiated from âstatic _OI std::__copy_normal<<anonymous>, <anonymous> >::copy_n(_II, _II, _OI) [with _II = const char*, _OI = const char*, bool <anonymous> = false, bool <anonymous> = false]â
/usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../include/c++/4.1.2/bits/stl_algobase.h:387:   instantiated from â_OutputIterator std::copy(_InputIterator, _InputIterator, _OutputIterator) [with _InputIterator = const char*, _OutputIterator = const char*]â
/usr/include/boost/algorithm/string/detail/replace_storage.hpp:87:   instantiated from âForwardIteratorT boost::algorithm::detail::process_segment_helper<HasStableIterators>::operator()(StorageT&, InputT&, ForwardIteratorT, ForwardIteratorT, ForwardIteratorT) [with StorageT = std::deque<char, std::allocator<char> >, InputT = const char [2], ForwardIteratorT = const char*, bool HasStableIterators = false]â
/usr/include/boost/algorithm/string/detail/replace_storage.hpp:150:   instantiated from âForwardIteratorT boost::algorithm::detail::process_segment(StorageT&, InputT&, ForwardIteratorT, ForwardIteratorT, ForwardIteratorT) [with StorageT = std::deque<char, std::allocator<char> >, InputT = const char [2], ForwardIteratorT = const char*]â
/usr/include/boost/algorithm/string/detail/find_format_all.hpp:221:   instantiated from âvoid boost::algorithm::detail::find_format_all_impl2(InputT&, FinderT, FormatterT, FindResultT, FormatResultT) [with InputT = const char [2], FinderT = boost::algorithm::detail::first_finderF<const char*, boost::algorithm::is_equal>, FormatterT = boost::algorithm::detail::empty_formatF<char>, FindResultT = boost::iterator_range<const char*>, FormatResultT = boost::algorithm::detail::empty_container<char>]â
/usr/include/boost/algorithm/string/detail/find_format_all.hpp:179:   instantiated from âvoid boost::algorithm::detail::find_format_all_impl(InputT&, FinderT, FormatterT, FindResultT) [with InputT = const char [2], FinderT = boost::algorithm::detail::first_finderF<const char*, boost::algorithm::is_equal>, FormatterT = boost::algorithm::detail::empty_formatF<char>, FindResultT = boost::iterator_range<const char*>]â
/usr/include/boost/algorithm/string/find_format.hpp:247:   instantiated from âvoid boost::algorithm::find_format_all(SequenceT&, FinderT, FormatterT) [with SequenceT = const char [2], FinderT = boost::algorithm::detail::first_finderF<const char*, boost::algorithm::is_equal>, FormatterT = boost::algorithm::detail::empty_formatF<char>]â
/usr/include/boost/algorithm/string/erase.hpp:588:   instantiated from âvoid boost::algorithm::erase_all(SequenceT&, const RangeT&) [with SequenceT = const char [2], RangeT = char [20]]â
test1.cpp:12:   instantiated from here
/usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../include/c++/4.1.2/bits/stl_algobase.h:317: error: no matching function for call to âstd::__copy<true, std::random_access_iterator_tag>::copy(const char*&, const char*&, const char*&)â


As well as the fact you put the inputs into boost::erase_all the wrong way round, what you are trying to achieve is impossible.

Both of what you entered as parameters are immutable arrays of bytes and cannot be modified by boost.

This should work:

std::string input("The quick brown fox");
boost::erase_all(input, " " );
std::cout << input << std::endl; // hopefully prints Thequickbrownfox


You have the arguments transposed and the input string needs to be non-const.

Change:

int main()
{
    erase_all(" ","the quick brown fox");
    return 1;
}

to:

int main()
{
    char s[] = "the quick brown fox";

    erase_all(s, " ");
    return 0;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜