【发布时间】:2021-08-05 07:47:46
【问题描述】:
我期待将 Python 对象传递给 Boost Python 类。这个对象有一个 ndarray 作为属性,我想把这个 ndarray 作为一个私有成员变量存储在这个类中,以便以后使用。我找不到合适的方法来执行此操作,并且在将 boost::python::numpy::ndarray 变量声明为私有时出现编译器错误。
这是我当前的代码:
#include <boost/python/numpy.hpp>
#include <boost/python.hpp>
namespace p = boost::python;
namespace np = boost::python::numpy;
class FlatlandCBS {
public:
FlatlandCBS(p::object railEnv) : m_railEnv(railEnv) {
// This is the Code I want to execute, so the ndarray is stored in the member varibale
p::object distance_map = p::extract<p::object>(railEnv.attr("distance_map"));
// Just a function which returns a ndarray (not the error)
map = p::extract<np::ndarray>(distance_map.attr("get")());
}
private:
p::object m_railEnv;
// Can't use this and I get a Compiler Error
np::ndarray map;
};
BOOST_PYTHON_MODULE(libFlatlandCBS) {
Py_Initialize();
np::initialize();
using namespace boost::python;
class_<FlatlandCBS>("FlatlandCBS", init<object>());
}
产生的错误信息是:
error: no matching function for call to ‘boost::python::numpy::ndarray::ndarray()’
这也是我的 CMakeLists.txt,因此您可能会重现此错误:
cmake_minimum_required (VERSION 3.8)
project (libFlatlandCBS)
# Add all the files to the library so it can get created
ADD_LIBRARY(FlatlandCBS SHARED
main.cpp)
# Set the Flags and the CXX command
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra -fconcepts")
INCLUDE_DIRECTORIES(include)
set(boostPython python)
find_package(PythonInterp 3.6 REQUIRED)
find_package(PythonLibs 3.6 REQUIRED)
include_directories(${PYTHON_INCLUDE_DIRS})
set(Boost_USE_STATIC_LIBS OFF)
set(Boost_USE_MULTITHREADED ON)
set(Boost_USE_STATIC_RUNTIME OFF)
FIND_PACKAGE(Boost REQUIRED COMPONENTS system program_options numpy ${boostPython})
if(Boost_FOUND)
include_directories(${Boost_INCLUDE_DIRS})
target_link_libraries(FlatlandCBS ${Boost_LIBRARIES} ${PYTHON_LIBRARIES})
else()
message(FATAL_ERROR "Could not find boost.")
endif()
【问题讨论】:
标签: python c++ numpy boost boost-python