2012-10-24 3 views
2

다음 코드가 작동하지 않는 이유가 궁금합니다. 다음 그 뒤에 주요 아이디어가 될 때 : STD 할당에 의해 상속 및 부스트 : 간 :: 할당을 각각 내 사용자 정의 클래스를 사용하려면메모리 할당자를 생성 할 때 C++ 상속 오류가 발생했습니다.

  1. 그리고는 기본 할당 자 대신를 사용하려면
  2. 클래스 MyStdAllocator를 만들면 std :: allocator에 의해 상속되고 std :: allocator (변수 x1 및 x2 참조) 대신 사용되어 작동하고 아무 경고도없고 오류도 발생하지 않습니다.
  3. boost :: interprocess :: allocator로 상속받은 MyBoostAllocator 클래스를 만들고 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; 
}; 

사용 가능할 때까지 파생 클래스에 생성자를 씁니다.

관련 문제