我正在将c ++类包装成一个so文件,然后让python调用它。
包装派生类。哪个失败了,因为它的基类具有私有的纯虚函数,有人可以帮忙吗?
a.h
#ifndef A_H_
#define A_H_
#include <iostream>
#include <stdio.h>
class A{
public:
A();
void a();
virtual void v();
private:
virtual void q() = 0;
};
#endif // A_H_
b.h
#ifndef B_H_
#define B_H_
#include <iostream>
#include <stdio.h>
#include "a.h"
class B : public A {
public:
B();
virtual void b();
void v();
private:
void q();
};
#endif // B_H_
a.cpp
#include <iostream>
#include <stdio.h>
#include "a.h"
A::A() {
printf("Init A class!\n");
}
void A::a() {
printf("A:a() called!\n");
}
void A::v() {
printf("A:v() called!\n");
}
b.cpp
#include <iostream>
#include <stdio.h>
#include "b.h"
#include "wrapb.h"
B::B() {
printf("Init B class!\n");
}
void B::b() {
printf("B:b() called!\n");
}
void B::v() {
printf("B:v() called!\n");
}
void B::q() {
printf("B:q() called!\n");
}
wrap.h
#include "./a.h"
#include "./b.h"
#include <boost/python.hpp>
using namespace boost::python;
// A * factoryA() { return new A; }
B * factoryB() { return new B; }
BOOST_PYTHON_MODULE(B)
{
using namespace boost::python;
class_<A>("A")
.def("a", &A:a);
.def("v", &A:v);
.def("q", pure_virtual(&A:q));
class_<B, bases<A> >("B")
.def("v", &B::v);
def("factoryB", factoryB,
return_value_policy<manage_new_object>());
}
编译命令: g ++ -fPIC -shared -o liba.so a.cpp g ++ -fPIC -shared -o B.so b.cpp -lboost_python -I / usr / include / python2.7 -I / root / anaconda2 / include / python2.7 -L。 -la
错误消息:
>In file included from b.cpp:4:0:
wrapb.h: In function ‘void init_module_B()’:
wrapb.h:14:19: error: found ‘:’ in nested-name-specifier, expected ‘::’
.def("a", &A:a);
^
wrapb.h:15:7: error: expected primary-expression before ‘.’ token
.def("v", &A:v);
^
wrapb.h:15:19: error: found ‘:’ in nested-name-specifier, expected ‘::’
.def("v", &A:v);
^
wrapb.h:16:7: error: expected primary-expression before ‘.’ token
.def("q", pure_virtual(&A:q));
^
wrapb.h:16:32: error: found ‘:’ in nested-name-specifier, expected ‘::’
.def("q", pure_virtual(&A:q));
^
In file included from b.h:5:0,
from b.cpp:3:
a.h:12:16: error: ‘virtual void A::q()’ is private
virtual void q() = 0;
^
In file included from b.cpp:4:0:
wrapb.h:16:33: error: within this context
.def("q", pure_virtual(&A:q));
^
In file included from /usr/include/boost/python/object/class_metadata.hpp:11:0,
from /usr/include/boost/python/class.hpp:23,
from /usr/include/boost/python.hpp:18,
from wrapb.h:4,
from b.cpp:4:
/usr/include/boost/python/object/value_holder.hpp: In instantiation of ‘struct boost::python::objects::value_holder<A’:
/usr/include/boost/type_traits/alignment_of.hpp:83:4: required from ‘const size_t boost::detail::alignment_of_impl<boost::python::objects::value_holder<A> >::value’
/usr/include/boost/type_traits/alignment_of.hpp:89:1: required from ‘struct boost::alignment_of<boost::python::objects::value_holder<A> >’
/usr/include/boost/python/object/instance.hpp:30:13: required from ‘struct boost::python::objects::instance<boost::python::objects::value_holder<A> >’
/usr/include/boost/python/object/instance.hpp:44:5: required from ‘const size_t boost::python::objects::additional_instance_size<boost::python::objects::value_holder<A> >::value’
/usr/include/boost/python/class.hpp:500:9: required from ‘void boost::python::class_<T, X1, X2, X3>::initialize(const DefVisitor&) [with DefVisitor = boost::python::init<>; W = A; X1 = boost::python::detail::not_specified; X2 = boost::python::detail::not_specified; X3 = boost::python::detail::not_specified]’
/usr/include/boost/python/class.hpp:630:5: required from ‘boost::python::class_<T, X1, X2, X3>::class_(const char*, const char*) [with W = A; X1 = boost::python::detail::not_specified; X2 = boost::python::detail::not_specified; X3 = boost::python::detail::not_specified]’
wrapb.h:13:18: required from here
/usr/include/boost/python/object/value_holder.hpp:66:11: error: cannot declare field ‘boost::python::objects::value_holder<A>::m_held’ to be of abstract type ‘A’
Value m_held;
^
In file included from b.h:5:0,
from b.cpp:3:
a.h:6:7: note: because the following virtual functions are pure within ‘A’:
class A{
^
a.h:12:16: note: virtual void A::q()
virtual void q() = 0;
^
In file included from /usr/include/boost/python/to_python_converter.hpp:11:0,
from /usr/include/boost/python/object/class_wrapper.hpp:8,
from /usr/include/boost/python/object/class_metadata.hpp:9,
from /usr/include/boost/python/class.hpp:23,
from /usr/include/boost/python.hpp:18,
from wrapb.h:4,
from b.cpp:4:
/usr/include/boost/python/converter/as_to_python_function.hpp: In instantiation of ‘struct boost::python::converter::as_to_python_function<A, boost::python::objects::class_cref_wrapper<A, boost::python::objects::make_instance<A, boost::python::objects::value_holder<A> > > >’:
/usr/include/boost/python/to_python_converter.hpp:88:9: required from ‘boost::python::to_python_converter<T, Conversion, has_get_pytype>::to_python_converter() [with T = A; Conversion = boost::python::objects::class_cref_wrapper<A, boost::python::objects::make_instance<A, boost::python::objects::value_holder<A> > >; bool has_get_pytype = true]’
/usr/include/boost/python/object/class_wrapper.hpp:24:8: required from ‘static void boost::python::objects::class_metadata<T, X1, X2, X3>::maybe_register_class_to_python(T2*, mpl_::false_) [with T2 = A; T = A; X1 = boost::python::detail::not_specified; X2 = boost::python::detail::not_specified; X3 = boost::python::detail::not_specified; mpl_::false_ = mpl_::bool_<false>]’
/usr/include/boost/python/object/class_metadata.hpp:229:80: required from ‘static void boost::python::objects::class_metadata<T, X1, X2, X3>::register_aux2(T2*, Callback) [with T2 = A; Callback = boost::integral_constant<bool, false>; T = A; X1 = boost::python::detail::not_specified; X2 = boost::python::detail::not_specified; X3 = boost::python::detail::not_specified]’
/usr/include/boost/python/object/class_metadata.hpp:219:60: required from ‘static void boost::python::objects::class_metadata<T, X1, X2, X3>::register_aux(void*) [with T = A; X1 = boost::python::detail::not_specified; X2 = boost::python::detail::not_specified; X3 = boost::python::detail::not_specified]’
/usr/include/boost/python/object/class_metadata.hpp:205:43: required from ‘static void boost::python::objects::class_metadata<T, X1, X2, X3>::register_() [with T = A; X1 = boost::python::detail::not_specified; X2 = boost::python::detail::not_specified; X3 = boost::python::detail::not_specified]’
/usr/include/boost/python/class.hpp:497:29: required from ‘void boost::python::class_<T, X1, X2, X3>::initialize(const DefVisitor&) [with DefVisitor = boost::python::init<>; W = A; X1 = boost::python::detail::not_specified; X2 = boost::python::detail::not_specified; X3 = boost::python::detail::not_specified]’
/usr/include/boost/python/class.hpp:630:5: required from ‘boost::python::class_<T, X1, X2, X3>::class_(const char*, const char*) [with W = A; X1 = boost::python::detail::not_specified; X2 = boost::python::detail::not_specified; X3 = boost::python::detail::not_specified]’
wrapb.h:13:18: required from here
/usr/include/boost/python/converter/as_to_python_function.hpp:21:17: error: cannot allocate an object of abstract type ‘A’
static void convert_function_must_take_value_or_const_reference(U(*)(T), int, T* = 0) {}
^
In file included from b.h:5:0,
from b.cpp:3:
a.h:6:7: note: since type ‘A’ has pure virtual functions
class A{