Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/291.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Boost.Python上没有匹配的函数_Python_C++_Boost_Boost Python - Fatal编程技术网

Boost.Python上没有匹配的函数

Boost.Python上没有匹配的函数,python,c++,boost,boost-python,Python,C++,Boost,Boost Python,所以这些天我一直在学习Boost.Python,但我遇到了这个问题,不明白为什么会发生这种情况,也不知道如何解决它。我为复制此问题而编写的伪代码如下: ... 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&g

所以这些天我一直在学习Boost.Python,但我遇到了这个问题,不明白为什么会发生这种情况,也不知道如何解决它。我为复制此问题而编写的伪代码如下:

...
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
#包括
#包括
#包括
使用名称空间boost::python;
结构A{
私人:
双val;
公众:
A(双val){
这->val=val;
}
double get(){
返回此->val;
}
无效集(双val){
这->val=val;
}
};
结构B{
私人:
std::map dict;
公众:
B();
双get(标准::字符串键){
返回此->dict[key].get();
}
无效集(标准::字符串键,双val){
此->dict[key]=A(val);
}
};
BOOST_PYTHON_模块(核心){
B类
.def(“get”,&B::get)
.def(“设置”、&B::设置);
}
我使用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
文件如下所示:

从setuptools导入设置中,查找\u包
从setuptools.extension导入扩展
扩展=[
延伸(
“BoostTest.core”,
sources=[“BoostTest/core.cpp”],
库=[“boost_python3”],
),
]
设置(
name='BoostTest',
packages=find_packages(),
ext_模块=扩展,
)
最后,我得到的输出如下:

...
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
。。。
BoostTest/core.cpp:30:26:从这里开始需要
/usr/include/c++/8/tuple:1668:70:错误:对“A::A()”的调用没有匹配的函数
第二个(std::forward(std::get(_tuple2))…)
^
BoostTest/core.cpp:13:3:note:candidate:'A::A(double)'
A(双val){
^
BoostTest/core.cpp:13:3:注意:候选者需要1个参数,提供0
BoostTest/core.cpp:9:8:note:candidate:'constexpr A::A(const A&)'
结构A{
^
BoostTest/core.cpp:9:8:注意:候选者需要1个参数,提供0
BoostTest/core.cpp:9:8:note:candidate:'constexpr A::A(A&&)'
BoostTest/core.cpp:9:8:注意:候选者需要1个参数,提供0

在这种情况下,我希望只有
B
暴露在Python中。出于这个原因,
A
不在
BOOST\u Python\u模块上。无论如何,我已经尝试在那里添加
A
,但它不起作用。我是BOOST.Python的新手,你们能帮帮我吗?

B::get
中,你们有一行
返回这个->dict[key].get();
。这比您最初想象的要复杂。尤其是
[]
映射的运算符将在元素不存在时插入元素。为此,它需要能够默认构造此新元素。由于映射将
a
对象存储为值,因此类
a
需要默认可构造

只需将默认构造函数添加到
类a

A(): val(0) { }

这可能解决了我给出的这个示例中的问题,但是,如果我需要仅使用参数构造对象,这会起作用吗?@GabrielMilan然后您需要在映射中以不同的方式查找元素。可以使用
dict.find(key)
(如果找不到密钥,请进行适当的处理)或
dict.at(key)
(如果找不到密钥,将引发异常)。