2016-04-10 13 views
1

、私は次のエラー受け付けております純粋仮想クラスから継承:ブーストのPythonモジュールがないとブーストのPythonブーストPythonモジュールで純粋仮想クラスから派生クラスを使用する場合は

allocating an object of abstract class type

を、エラーではありませんプレゼント。次の例(私のユースケースに近い)では、何が問題になるでしょうか?私は、BaseDerivedの間の継承関係をPythonに認識させる必要がありますか?

これは(あなたがいずれかのpythonにBaseまたはDerivedを露出していない)まったくのpythonとは関係ありません

#Set CMake Version and project name. 
cmake_minimum_required(VERSION 2.8) 
project(PythonMinimal) 

#Attempt to find Python and Boost Python. 
find_package(PythonInterp) 
find_package(PythonLibs) 
find_package(Boost COMPONENTS python) 

#Find includes. 
include_directories(${Boost_INCLUDE_DIRS} ${PYTHON_INCLUDE_DIRS}) 

#Add library to project. 
add_library(PythonMinimal SHARED PythonMinimal.cpp) 
target_link_libraries(PythonMinimal ${Boost_LIBRARIES} ${PYTHON_LIBRARIES}) 

答えて

2

PythonMinimal.cpp

#include <boost/python.hpp> 
#include <vector> 
using namespace boost::python; 

//Base class - pure virtual. 
template<typename T> 
class Base{ 
public: 
virtual void foo() = 0; 
}; 

//Derived class, inherites from Base. 
template<typename T> 
class Derived : public Base<T>{ 
public: 
    Derived(int a, int b){ 
     //Do something. 
    } 

    void foo(){ 
     //Do something else. 
    } 
}; 

//Test class, uses instances of Derived stored in STL container of Base. 
template<typename T> 
class TestClass{ 
public: 
    std::vector< Base<T> > vec; 

    TestClass(int a, int b){ 
     vec.push_back(Derived<T>(a, b)); 
    } 

    void foo(){ 
     vec.at(0).foo(); 
    } 
}; 

//Build python module. 
BOOST_PYTHON_MODULE(cpuMLP){ 
    class_< TestClass<float> >("TestClass", init<int, int>()) 
     .def("foo", &TestClass<float>::foo); 
}; 

CMakeLists.txt問題はあなたのものですvector

std::vector< Base<T> > vec; 

Base<T>は、の値をとしている抽象クラスです。それはうまくいきません。あなたはポインタでそれらを格納する必要があります:

std::vector<std::unique_ptr<Base<T>>> vec; 

あなたDerived<T>オブジェクトをスライスしていないこの方法です。

+0

もちろん、私には明らかだったはずです!今私は馬鹿だと感じる。ありがとうございました! –

関連する問題