[英]No matching function on Boost.Python
所以这些天我一直在学习 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,
)
最后,我得到的output如下:
...
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 的新手,你们能帮帮我吗?
在B::get
中,只有一行return this->dict[key].get();
. 这比您最初想象的要复杂。 特别是,map 的[]
运算符将插入一个不存在的元素。 为此,它需要能够默认构造这个新元素。 由于您的 map 将A
对象存储为值,因此您的 class A
需要默认可构造。
只需将默认构造函数添加到class A
:
A(): val(0) { }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.