[英]No matching function on Boost.Python
So I've been learning Boost.Python these days but I've faced this issue and can't understand why this happens nor how to fix it.所以这些天我一直在学习 Boost.Python 但我遇到了这个问题,不明白为什么会发生这种情况,也不知道如何解决它。 A dummy code I've wrote in order to replicate this issue is the following:
我为复制此问题而编写的虚拟代码如下:
#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);
}
I'm using setuptools extensions in order to compile it, so the following compilation command is generated automatically:我正在使用 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
Just in case you're wondering, my setup.py
file looks like this:万一您想知道,我的
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,
)
Finally, the output I get is the following:最后,我得到的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
In this case, I'd like that only B
was exposed to Python.在这种情况下,我希望只有
B
暴露于 Python。 For that reason, A
is not on BOOST_PYTHON_MODULE
.因此,
A
不在BOOST_PYTHON_MODULE
上。 Anyway, I've tried adding A
there but it doesn't work.无论如何,我已经尝试在那里添加
A
但它不起作用。 I'm pretty newbie to Boost.Python, can you guys please help me?我是 Boost.Python 的新手,你们能帮帮我吗?
In B::get
, you have a one line return this->dict[key].get();
在
B::get
中,只有一行return this->dict[key].get();
. . This is more complicated than you may initially think.
这比您最初想象的要复杂。 In particular, the
[]
operator for a map will insert an element if it does not exist.特别是,map 的
[]
运算符将插入一个不存在的元素。 For it to do this, it needs to be able to default construct this new element.为此,它需要能够默认构造这个新元素。 Since your map stores
A
objects as values, your class A
needs to be default constructable.由于您的 map 将
A
对象存储为值,因此您的 class A
需要默认可构造。
Simply add a default constructor to class A
:只需将默认构造函数添加到
class A
:
A(): val(0) { }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.