【发布时间】:2020-12-10 08:13:36
【问题描述】:
所以这些天我一直在学习 Boost.Python,但我遇到了这个问题,不明白为什么会发生这种情况,也不知道如何解决它。我为复制此问题而编写的虚拟代码如下:
#include <map>
#include <string>
#include <boost/python.hpp>
using namespace boost::python;
struct A {
private:
double val;
public:
A (double val) {
this->val = val;
}
double get() {
return this->val;
}
void set(double val) {
this->val = val;
}
};
struct B {
private:
std::map<std::string, A> dict;
public:
B ();
double get(std::string key) {
return this->dict[key].get();
}
void set(std::string key, double val) {
this->dict[key] = A(val);
}
};
BOOST_PYTHON_MODULE(core) {
class_<B>("B")
.def("get", &B::get)
.def("set", &B::set);
}
我正在使用 setuptools 扩展来编译它,所以会自动生成以下编译命令:
x86_64-linux-gnu-gcc -pthread -DNDEBUG -g -fwrapv -O2 -Wall -g -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -fPIC -I/home/gabriel-milan/sandbox/BoostTest/ -I/usr/include/python3.7m -c BoostTest/core.cpp -o build/temp.linux-x86_64-3.7/BoostTest/core.o
如果您想知道,我的 setup.py 文件如下所示:
from setuptools import setup, find_packages
from setuptools.extension import Extension
extensions = [
Extension(
"BoostTest.core",
sources=["BoostTest/core.cpp"],
libraries=["boost_python3"],
),
]
setup (
name='BoostTest',
packages=find_packages(),
ext_modules=extensions,
)
最后,我得到的输出如下:
...
BoostTest/core.cpp:30:26: required from here
/usr/include/c++/8/tuple:1668:70: error: no matching function for call to ‘A::A()’
second(std::forward<_Args2>(std::get<_Indexes2>(__tuple2))...)
^
BoostTest/core.cpp:13:3: note: candidate: ‘A::A(double)’
A (double val) {
^
BoostTest/core.cpp:13:3: note: candidate expects 1 argument, 0 provided
BoostTest/core.cpp:9:8: note: candidate: ‘constexpr A::A(const A&)’
struct A {
^
BoostTest/core.cpp:9:8: note: candidate expects 1 argument, 0 provided
BoostTest/core.cpp:9:8: note: candidate: ‘constexpr A::A(A&&)’
BoostTest/core.cpp:9:8: note: candidate expects 1 argument, 0 provided
在这种情况下,我希望只有 B 暴露于 Python。因此,A 不在BOOST_PYTHON_MODULE 上。无论如何,我已经尝试在那里添加A,但它不起作用。我是 Boost.Python 的新手,你们能帮帮我吗?
【问题讨论】:
标签: python c++ boost boost-python