2012-10-24 30 views
2

我想知道爲什麼下面的代碼不起作用。 其背後的主要思路如下:創建內存分配器時C++繼承錯誤

  1. 我想用我的自定義類,通過STD分配器繼承和分別提高::進程間::分配,我想,而不是使用它們的基礎分配器的
  2. 當我創建一個MyStdAllocator類,它通過std :: allocator繼承並使用它來代替std :: allocator(請參閱變量x1和x2),它可以工作,並且不會發出警告和錯誤。
  3. 當我創建一個類MyBoostAllocator,它通過boost :: interprocess :: allocator繼承,並使用它而不是boost :: interprocess:allocator(請參閱變量y1和y2)它不起作用,並給我列出編譯錯誤這個問題的底部。

請您告訴我,爲什麼這樣的遺傳不起作用,以及如何解決它?

#include <boost/interprocess/managed_shared_memory.hpp> 
#include <boost/interprocess/allocators/allocator.hpp> 
#include <iostream> 

using namespace boost::interprocess; 


template <typename _Tp> 
class MyStdAllocator : public std::allocator<_Tp> 
{ 
}; 


template<class T, class SegmentManager> 
class MyBoostAllocator : public allocator<T, SegmentManager> 
{ 
}; 



typedef std::allocator<int> std_allocator; 
typedef MyStdAllocator<int> my_std_allocator; 

typedef allocator  <int, managed_shared_memory::segment_manager> boost_allocator; 
typedef MyBoostAllocator<int, managed_shared_memory::segment_manager> my_boost_allocator; 


int main(int argc, char** argv) { 

    struct shm_remove { 
     shm_remove() { 
      shared_memory_object::remove("MySharedMemory"); 
     } 

     ~shm_remove() { 
      shared_memory_object::remove("MySharedMemory"); 
     } 
    } remover; 

    managed_shared_memory segment(create_only, 
      "MySharedMemory", //segment name 
      65536); 

    //Create an allocator that allocates ints from the managed segment 
    allocator<int, managed_shared_memory::segment_manager> 
      allocator_instance(segment.get_segment_manager()); 


    std_allocator *x1; 
    x1 = new std_allocator(); 
    delete x1; 

    my_std_allocator *x2; 
    x2 = new my_std_allocator(); 
    delete x2; 


    boost_allocator *y1; 
    y1 = new boost_allocator(segment.get_segment_manager()); 
    delete y1; 

    // following lines generate compilation errors: 
    my_boost_allocator *y2; 
    y2 = new my_boost_allocator(segment.get_segment_manager()); 
    delete y2; 

    std::cout<<"its working!\n"; 

    return 0; 

} 

的編譯錯誤如下:

../src/test.cpp:73:59: error: no matching function for call to ‘MyBoostAllocator<int, boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family>, boost::interprocess::iset_index> >::MyBoostAllocator(boost::interprocess::ipcdetail::basic_managed_memory_impl<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family>, boost::interprocess::iset_index, 16ul>::segment_manager*)’ 
../src/test.cpp:73:59: note: candidates are: 
../src/test.cpp:24:7: note: MyBoostAllocator<int, boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family>, boost::interprocess::iset_index> >::MyBoostAllocator() 
../src/test.cpp:24:7: note: candidate expects 0 arguments, 1 provided 
../src/test.cpp:24:7: note: MyBoostAllocator<int, boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family>, boost::interprocess::iset_index> >::MyBoostAllocator(const MyBoostAllocator<int, boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family>, boost::interprocess::iset_index> >&) 
../src/test.cpp:24:7: note: no known conversion for argument 1 from ‘boost::interprocess::ipcdetail::basic_managed_memory_impl<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family>, boost::interprocess::iset_index, 16ul>::segment_manager* {aka boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family>, boost::interprocess::iset_index>*}’ to ‘const MyBoostAllocator<int, boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family>, boost::interprocess::iset_index> >&’ 
make: *** [src/test.o] Error 1 
+1

那麼,正如錯誤所說,你的分配器類沒有一個與你的初始化器相匹配的構造器。 –

+0

好的,但爲什麼構造函數沒有自動繼承?當通過基類繼承時,甚至有可能用C++繼承它,它提供了這樣的構造函數? –

+1

@ danilo2構造函數不是以C++(或我知道的任何其他語言)繼承的。 – user1610015

回答

1

這似乎是一個 「支持C++ 11」 的問題。見

http://wiki.apache.org/stdcxx/C++0xCompilerSupport

針對編譯器支持哪些功能。

Inheriting constructors僅限於gnu4.8 +。

當他們仍然必須顯式地使用使用構造。

template<class T, class SegmentManager> 
class MyBoostAllocator : public allocator<T, SegmentManager> 
{ 
public: 
    using allocator<T, SegmentManager >::allocator; 
}; 

在可用之前,將構造函數寫入派生類。