首页 > 解决方案 > Boost.Python ArgumentError 与类指针不匹配 C++ 签名

问题描述

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

问题是:

如何使用 的功能the_a

包括/a.hpp

#ifndef _A_HPP_
#define _A_HPP_
#include <iostream>

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

#endif //_A_HPP_

包括/the_a.hpp

#ifndef _THE_A_
#define _THE_A_ 

#include "a.hpp"

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

#endif 

包括/b.hpp

#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/a.cpp

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

src/the_a.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;
}

src/b.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);
}

boost python 包装文件

内部成员指针.cpp

#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>())
        ;



}

测试cpp代码的主要功能

主文件

/*************************************************************************
    > 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;
}

CMakeLiss.txt

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)

一个python测试脚本

t.py

#/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

python输出结果

1000
<inner_member_pointer.PyTheA object at 0x7f634644be68>
<class 'inner_member_pointer.PyTheA'>
the a
1280
<inner_member_pointer.PyTheA object at 0x7f63464a1520>
<class 'inner_member_pointer.PyTheA'>
Traceback (most recent call last):
  File "t.py", line 23, in <module>
    the_a.capImage()
Boost.Python.ArgumentError: Python argument types in
    PyTheA.capImage(PyTheA)
did not match C++ signature:
    capImage(TheA {lvalue})

代码托管在:

https://github.com/wangzheqie/cpp_python_convert

目录boost_python

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

解决方案


推荐阅读