我不明白g ++错误。 我尝试编译简单的代码:
#include <string>
#include <boost/interprocess/sync/interprocess_mutex.hpp>
#include <boost/interprocess/managed_shared_memory.hpp>
#include <boost/interprocess/allocators/allocator.hpp>
#include <boost/interprocess/containers/vector.hpp>
#include <boost/interprocess/containers/string.hpp>
#include <boost/interprocess/containers/list.hpp>
#include <boost/interprocess/containers/map.hpp>
namespace BIP = boost::interprocess;
using std::pair;
using std::less;
using std::char_traits;
class Edge {
public:
typedef BIP::managed_shared_memory::segment_manager segment_manager_t;
typedef BIP::allocator<void, segment_manager_t> void_allocator_t;
typedef BIP::allocator<char, segment_manager_t> char_allocator_t;
typedef BIP::basic_string<char, char_traits<char>, char_allocator_t> char_string_t;
typedef BIP::vector<char_string_t, char_allocator_t> vector_string_t;
Edge(long int endNodeId, const char_allocator_t & alloc);
Edge();
Edge(const Edge & other);
~Edge();
void addComment(const std::string & comment);
private:
vector_string_t comments;
long int endNodeId;
int count;
};
inline void Edge::addComment(const std::string & comment) {
char_string_t newComment(comment.c_str(), comments.get_allocator());
comments.push_back(newComment); **// all is good without this line**
}
g ++ out(gcc版本4.7.3(Debian 4.7.3-4)):
In file included from /usr/include/boost/interprocess/containers/vector.hpp:19:0,
from forStackOverflow.hpp:6:
/usr/include/boost/container/vector.hpp: In instantiation of ‘void boost::container::vector<T, Allocator>::priv_push_back(const T&) [with T = boost::container::basic_string<char, std::char_traits<char>, boost::interprocess::allocator<char, boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family>, boost::interprocess::iset_index> > >; Allocator = boost::interprocess::allocator<char, boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family>, boost::interprocess::iset_index> >]’:
/usr/include/boost/container/vector.hpp:1371:4: required from ‘void boost::container::vector<T, Allocator>::push_back(T&) [with T = boost::container::basic_string<char, std::char_traits<char>, boost::interprocess::allocator<char, boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family>, boost::interprocess::iset_index> > >; Allocator = boost::interprocess::allocator<char, boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family>, boost::interprocess::iset_index> >]’
forStacknckOverflow.hpp:45:31: required from here
/usr/include/boost/container/vector.hpp:1788:92: error: no matching function for call to ‘boost::container::container_detail::insert_copy_proxy<boost::interprocess::allocator<char, boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family>, boost::interprocess::iset_index> >, boost::container::basic_string<char, std::char_traits<char>, boost::interprocess::allocator<char, boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family>, boost::interprocess::iset_index> >
>*>::insert_copy_proxy(boost::interprocess::allocator<char, boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family>, boost::interprocess::iset_index> >&, const boost::container::basic_string<char, std::char_traits<char>, boost::interprocess::allocator<char, boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family>, boost::interprocess::iset_index> > >&)’
/usr/include/boost/container/vector.hpp:1788:92: note: candidates are:
In file included from /usr/include/boost/container/vector.hpp:50:0,
from /usr/include/boost/interprocess/containers/vector.hpp:19,
from forStackOverflow.hpp:6:
/usr/include/boost/container/detail/advanced_insert_int.hpp:132:4: note: boost::container::container_detail::insert_copy_proxy<A, Iterator>::insert_copy_proxy(A&, const value_type&) [with A = boost::interprocess::allocator<char, boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family>, boost::interprocess::iset_index> >; Iterator = boost::container::basic_string<char, std::char_traits<char>, boost::interprocess::allocator<char, boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family>, boost::interprocess::iset_index> > >*; boost::container::container_detail::insert_copy_proxy<A, Iterator>::value_type = char]
/usr/include/boost/container/detail/advanced_insert_int.hpp:132:4: note: no known conversion for argument 2 from ‘const boost::container::basic_string<char, std::char_traits<char>, boost::interprocess::allocator<char, boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family>, boost::interprocess::iset_index> > >’ to ‘const value_type& {aka const char&}’
/usr/include/boost/container/detail/advanced_insert_int.hpp:126:8: note: boost::container::container_detail::insert_copy_proxy<boost::interprocess::allocator<char, boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family>, boost::interprocess::iset_index> >, boost::container::basic_string<char, std::char_traits<char>, boost::interprocess::allocator<char, boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family>, boost::interprocess::iset_index> > >*>::insert_copy_proxy(const boost::container::container_detail::insert_copy_proxy<boost::interprocess::allocator<char, boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family>, boost::interprocess::iset_index> >, boost::container::basic_string<char, std::char_traits<char>, boost::interprocess::allocator<char, boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family>, boost::interprocess::iset_index> > >*>&)
/usr/include/boost/container/detail/advanced_insert_int.hpp:126:8: note: candidate expects 1 argument, 2 provided
In file included from /usr/include/boost/interprocess/segment_manager.hpp:33:0,
from /usr/include/boost/interprocess/detail/managed_memory_impl.hpp:26,
from /usr/include/boost/interprocess/managed_shared_memory.hpp:21,
from forStackOverflow.hpp:4:
/usr/include/boost/interprocess/allocators/allocator.hpp: In instantiation of ‘void boost::interprocess::allocator<T, SegmentManager>::construct(const pointer&, const P&) [with P = boost::container::basic_string<char, std::char_traits<char>, boost::interprocess::allocator<char, boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family>, boost::interprocess::iset_index> > >; T = char; SegmentManager = boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family>, boost::interprocess::iset_index>; boost::interprocess::allocator<T, SegmentManager>::pointer = boost::interprocess::offset_ptr<char, long int, long unsigned int, 0ul>]’:
/usr/include/boost/preprocessor/iteration/detail/local.hpp:37:1: recursively required from ‘static void boost::container::allocator_traits<Alloc>::priv_construct_dispatch2(boost::true_type, Alloc&, T*, const P0&) [with T = char; P0 = boost::container::basic_string<char, std::char_traits<char>, boost::interprocess::allocator<char, boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family>, boost::interprocess::iset_index> > >; Alloc = boost::interprocess::allocator<char, boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family>, boost::interprocess::iset_index> >; boost::true_type = boost::integral_constant<bool, true>]’
/usr/include/boost/preprocessor/iteration/detail/local.hpp:37:1: required from ‘static void boost::container::allocator_traits<Alloc>::priv_construct(boost::false_type, Alloc&, T*, const P0&) [with T = char; P0 = boost::container::basic_string<char, std::char_traits<char>, boost::interprocess::allocator<char, boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family>, boost::interprocess::iset_index> > >; Alloc = boost::interprocess::allocator<char, boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family>, boost::interprocess::iset_index> >; boost::false_type = boost::integral_constant<bool, false>]’
/usr/include/boost/preprocessor/iteration/detail/local.hpp:37:1: required from ‘static void boost::container::allocator_traits<Alloc>::construct(Alloc&, T*, const P0&) [with T = char; P0 = boost::container::basic_string<char, std::char_traits<char>, boost::interprocess::allocator<char, boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family>, boost::interprocess::iset_index> > >; Alloc = boost::interprocess::allocator<char, boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family>, boost::interprocess::iset_index> >]’
/usr/include/boost/container/vector.hpp:1781:10: required from ‘void boost::container::vector<T, Allocator>::priv_push_back(const T&) [with T = boost::container::basic_string<char, std::char_traits<char>, boost::interprocess::allocator<char, boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family>, boost::interprocess::iset_index> > >; Allocator = boost::interprocess::allocator<char, boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family>, boost::interprocess::iset_index> >]’
/usr/include/boost/container/vector.hpp:1371:4: required from ‘void boost::container::vector<T, Allocator>::push_back(T&) [with T = boost::container::basic_string<char, std::char_traits<char>, boost::interprocess::allocator<char, boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family>, boost::interprocess::iset_index> > >; Allocator = boost::interprocess::allocator<char, boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family>, boost::interprocess::iset_index> >]’
forStackOverflow.hpp:45:31: required from here
/usr/include/boost/interprocess/allocators/allocator.hpp:263:7: error: cannot convert ‘const boost::container::basic_string<char, std::char_traits<char>, boost::interprocess::allocator<char, boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family>, boost::interprocess::iset_index> > >’ to ‘boost::interprocess::allocator<char, boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family>, boost::interprocess::iset_index> >::value_type {aka char}’ in initialization
如果我评论53行,则错误消失。我尝试clang编译器,但它没有帮助。 谢谢!
编辑:添加所有错误消息。
答案 0 :(得分:2)
好的,从错误消息中我可以看到从boost::container::basic_string
到const char&
的转换错误
错误消息进一步指向boost标头中的位置,分配器正在尝试为要推回的新对象分配内存:
void priv_push_back(const T &x)
{
if (this->m_holder.m_size < this->m_holder.capacity()){
//There is more memory, just construct a new object at the end
allocator_traits_type::construct
( this->m_holder.alloc()
, container_detail::to_raw_pointer(this->m_holder.start() + this->m_holder.m_size)
, x );
++this->m_holder.m_size;
}
else{
container_detail::insert_copy_proxy<Allocator, T*> proxy(this->m_holder.alloc(), x);
this->priv_forward_range_insert_no_capacity(vector_iterator_get_ptr(this->cend()), 1, proxy, alloc_version());
}
}
这是出问题的地方。此函数中的类型T为boost::interprocess::basic_string
,因此当调用allocator_traits_type::construct
时,它会尝试将boost::interprocess::basic_string
传递给您声明为char
类型的分配器:
typedef BIP::allocator<char, segment_manager_t> char_allocator_t;
typedef BIP::vector<char_string_t, char_allocator_t> vector_string_t;
你的矢量应该有一个字符串分配器而不是char分配器,因为你的字符串向量想要分配字符串。