Boost.Python ArgumentError与类指针的C ++签名不匹配

时间:2018-06-21 03:10:47

标签: c++ python-2.7 boost boost-python

我有一个virtual class Asubclass of A, TheA和一个class B

  • TheA指针cam1在类B的{​​{1}}函数内部初始化
  • 我通过名为init()的{​​{1}}将这个cam1指针传递给python。
  • boost.python是一个the_a python对象,但无法使用其功能。
  • 使用ubuntu 16.04,cmake 3.5,python 2.7和boost 1.58.1
  

问题是:

     

如何使用the_a的功能?

include / a.hpp

PyTheA

include / the_a.hpp

the_a

include / b.hpp

#ifndef _A_HPP_
#define _A_HPP_
#include <iostream>

class A{
    public:
    virtual void capImage() = 0;
    virtual int getWidth() = 0;
};

#endif //_A_HPP_

src / a.cpp

#ifndef _THE_A_
#define _THE_A_ 

#include "a.hpp"

class TheA : public A{
    public:
    TheA();
    ~TheA();
    void capImage();
    int getWidth();
};

#endif 

src / the_a.cpp

#ifndef _B_HPP_
#define _B_HPP_
#include <iostream>
#include "a.hpp"
class B{
    public:
    A *cam1;
    A *cam2;

    public:
    void init();
    int getWidth() ;
    A*  getA();
};
#endif

src / b.cpp

#include <iostream>
#include "a.hpp"
  

boost python包装文件

inner_member_pointer.cpp

#include "the_a.hpp"
#include <iostream>

using namespace std;
TheA::TheA(){
    ;
}
TheA::~TheA(){
    ;
}
void TheA::capImage(){
    cout<<"the a"<<endl;
}

int TheA::getWidth(){
    return 1280;
}
  

测试cpp代码的主要功能

main.cpp

#include "b.hpp"
#include "a.hpp"
#include "the_a.hpp"
void B::init(){
    A *a = new TheA();
    this->cam1 = new TheA();
}
int B::getWidth() {
    return 1000;
}
A* B::getA(){
    return this->cam1;
    //or
    // return this->(*cam1);
}

CMakeLiss.txt

#include <iostream>
#include <boost/python.hpp>
#include <boost/python/wrapper.hpp>
#include <boost/python/object.hpp>
#include <boost/python/instance_holder.hpp>

#include "a.hpp"
#include "b.hpp"
#include "the_a.hpp"

using namespace std;
using namespace boost;
using namespace boost::python;

struct AWrapper: A, wrapper<A>{
    void capImage(){
        this->get_override("capImage")();
    }
    int getWidth(){
        return this->get_override("getWidth")();
    }
};

BOOST_PYTHON_MODULE(inner_member_pointer){
    class_<AWrapper, boost::noncopyable>("PyA")
        .def("capImage", pure_virtual(&A::capImage))
        .def("getWidth", pure_virtual(&A::getWidth))
        ;
    class_<TheA, boost::shared_ptr<TheA> >("PyTheA")
        .def("capImage", &TheA::capImage)
        .def("getWidth", &TheA::getWidth)
        ;

    class_<B>("PyB")
        .def("getWidth", &B::getWidth)
        .def("init", &B::init)
        .def("getA", &B::getA, return_value_policy<manage_new_object>())
        ;



}
  

python测试脚本

t.py

/*************************************************************************
    > File Name: main.cpp
    > Author: wangzheqie At: https://github.com/wangzheqie
    > Mail: wangzheqie@qq.com
    > Created Time: Thu 21 Jun 2018 08:06:34 AM CST
************************************************************************/

#include <iostream>
#include "a.hpp"
#include "b.hpp"
#include "the_a.hpp"

using namespace std;


int main(int argc, char** argv){
    B b;
    b.init();
    cout<<b.getWidth()<<endl;
    cout<<b.getA()<<endl;

    TheA *the_a = (TheA*)b.getA(); 
    the_a->capImage();
    cout<<the_a->getWidth()<<endl;
    return 0;
}
  

python输出结果

cmake_minimum_required(VERSION 2.8)

find_package(Boost 1.5 REQUIRED)

set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-cpp")
include_directories(
    ${PROJECT_SOURCE_DIR}
    ${PROJECT_SOURCE_DIR}/include
    ${Boost_INCLUDE_DIRS}
    /usr/include/python2.7
)
link_directories(
    /usr/lib/x86_64-linux-gnu
    /usr/lib
    /usr/local/lib
)
add_library(libs SHARED 
    src/a.cpp
    src/b.cpp 
    src/the_a.cpp
    )
add_library(inner_member_pointer SHARED inner_member_pointer.cpp)
target_link_libraries(inner_member_pointer libs libboost_python.so libpython2.7.so)
set_target_properties(inner_member_pointer PROPERTIES PREFIX "")

add_executable(main main.cpp)
target_link_libraries(main libs)
  

代码托管在:

https://github.com/wangzheqie/cpp_python_convert

目录#/usr/bin/env python2.7 # -*- coding: utf-8 -*- from inner_member_pointer import PyB from inner_member_pointer import PyTheA from inner_member_pointer import PyA b = PyB() b.init() # if not init(), b.getA() is None print(b.getWidth()) # new a PyTheA in python a = PyTheA() print(a) print(type(a)) a.capImage() print(a.getWidth()) # return a PyThA from c++ the_a = b.getA() print(the_a) print(type(the_a)) the_a.capImage() # error print(the_a.getWidth()) # error

0 个答案:

没有答案