C++ boost向量的问题

C++ boost向量的问题,c++,boost,C++,Boost,我不理解g++错误。 我尝试编译简单的代码: #include <string> #include <boost/interprocess/sync/interprocess_mutex.hpp> #include <boost/interprocess/managed_shared_memory.hpp> #include <boost/interprocess/allocators/allocator.hpp> #include <bo

我不理解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**
}
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
名称空间BIP=boost::进程间;
使用std::pair;
使用std::less;
使用std::char_特征;
阶级边缘{
公众:
typedef BIP::托管共享内存::段管理器段管理器;
typedef BIP::分配器无效\u分配器\u t;
typedef BIP::分配器字符分配器;
typedef BIP::基本字符串字符;
typedef BIP::向量字符串;
边缘(长整数endNodeId、常量字符分配器和分配符);
边();
边缘(常数边缘和其他);
~Edge();
void addComment(const std::string和comment);
私人:
向量字符串注释;
长内端节;
整数计数;
};
内联void Edge::addComment(常量std::string和comment){
char_string_t newComment(comment.c_str(),comments.get_allocator());
评论。推回(新成员);**//没有这行,一切都好**
}
g++输出(gcc版本4.7.3(Debian 4.7.3-4)):

在/usr/include/boost/interprocess/containers/vector.hpp:19:0中包含的文件中,
来自forStackOverflow。水电站:6:
/usr/include/boost/container/vector.hpp:在“void boost::container::vector::priv_push_back(const T&)[with T=boost::container::basic_string;Allocator=boost::interprocess::Allocator]”的实例化中:
/usr/include/boost/container/vector.hpp:1371:4:来自“void boost::container::vector::push_back(T&)[with T=boost::container::basic_string;Allocator=boost::interprocess::Allocator]的必填项
ForstackKoverflow.hpp:45:31:此处要求
/usr/include/boost/container/vector.hpp:1788:92:错误:调用“boost::container::container\u detail::insert\u copy\u proxy::insert\u copy\u proxy(boost::进程间::分配器&,const boost::container::basic\u string&)”时没有匹配的函数
/usr/include/boost/container/vector.hpp:1788:92:注:候选项包括:
在/usr/include/boost/container/vector.hpp:50:0中包含的文件中,
来自/usr/include/boost/interprocess/containers/vector.hpp:19,
来自forStackOverflow。水电站:6:
/usr/include/boost/container/detail/advanced_insert_int.hpp:132:4:注:boost::container::container_detail::insert_copy_proxy::insert_copy_proxy(A&,const value_type&)[带A=boost::进程间::分配器;迭代器=boost::container::basic_string*;boost::container::container_detail::insert_copy_proxy::value_type=char]
/usr/include/boost/container/detail/advanced_insert_int.hpp:132:4:注意:参数2从'const boost::container::basic_string'到'const value_type&{aka const char&}没有已知的转换
/usr/include/boost/container/detail/advanced\u insert\u int.hpp:126:8:注意:boost::container::container\u detail::insert\u copy\u proxy::insert\u copy\u proxy(const boost::container::container\u detail::insert\u proxy&)
/usr/include/boost/container/detail/advanced_insert_int.hpp:126:8:注意:候选者需要1个参数,提供2个参数
在/usr/include/boost/interprocess/segment_manager.hpp:33:0中包含的文件中,
从/usr/include/boost/interprocess/detail/managed_memory_impl.hpp:26,
从/usr/include/boost/interprocess/managed_shared_memory.hpp:21,
来自forStackOverflow。水电站:4:
/usr/include/boost/interprocess/allocators/allocator.hpp:在“void boost::interprocess::allocator::const pointer&,const P&”的实例化中[P=boost::container::basic_string;T=char;SegmentManager=boost::interprocess::segment_manager;boost::interprocess::allocator::pointer=boost::interprocess::offset_ptr]:
/usr/include/boost/preprocessor/iteration/detail/local.hpp:37:1:从'static void boost::container::alloctor_traits::priv_construct_dispatch2'递归需要(boost::true_type,Alloc&,T*,const P0&)[带T=char;P0=boost::container::basic_string;Alloc=boost::进程间::分配器;boost::true_type=boost::integral_常量]’
/usr/include/boost/preprocessor/iteration/detail/local.hpp:37:1:static void boost::container::allocator\u traits::priv\u construct(boost::false\u type,Alloc&,T*,const P0&)[带T=char;P0=boost::container::basic\u string;Alloc=boost::interprocess::allocator;boost::false\u type=boost::integral\u常量]]
/usr/include/boost/preprocessor/iteration/detail/local.hpp:37:1:static void boost::container::allocator\u traits::construct(Alloc&,T*,const P0&)[with T=char;P0=boost::container::basic\u string;Alloc=boost::interprocess::allocator]'
/usr/include/boost/container/vector.hpp:1781:10:来自“void boost::container::vector::priv_push_back(const T&)[with T=boost::container::basic_string;Allocator=boost::interprocess::Allocator]的必填项
/usr/include/boost/container/vector.hpp:1371:4:来自“void boost::container::vector::push_back(T&)[with T=boost::container::basic_string;Allocator=boost::interprocess::Allocator]的必填项
forStackOverflow.hpp:45:31:从此处开始需要
/usr/include/boost/interprocess/allocators/allocator.hpp:263:7:错误:无法在初始化中将“const boost::container::basic_string”转换为“boost::interprocess::allocator::value_type{aka char}”
若我注释53行,那个么错误就会消失。我试着使用铿锵编译器,但没用。 谢谢


编辑:添加所有错误消息。

好的,从错误消息中我可以看到从
boost::container::basic_字符串
常量字符&

错误消息进一步指向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());
      }
   }
向量应该有一个字符串分配器,而不是一个字符分配器,因为
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());
      }
   }
typedef BIP::allocator<char, segment_manager_t> char_allocator_t;
typedef BIP::vector<char_string_t, char_allocator_t> vector_string_t;