小编sib*_*net的帖子

Cython 重载“找不到合适的方法”

我在通过 Cython 访问 C++ 类中的重载构造函数时遇到问题。我正在尝试按照此处所述包装 C++ 类。该类有多个参数数量相同的构造函数,仅类型不同,例如这里提到的类。但是,Cython 无法决定调用哪个构造函数,从而给出错误“找不到合适的方法”。

例如,给定 foo.h 如下

class Foo 
{
    public:
        int which_constructor;

        Foo(int){ which_constructor = 1; }
        Foo(bool){ which_constructor = 2; };

        ~Foo();
};
Run Code Online (Sandbox Code Playgroud)

和 pyfoo.pyx 如下

from libcpp cimport bool as bool_t

cdef extern from "foo.h":
    cdef cppclass Foo:
        Foo(int)
        Foo(bool_t)
        int which_constructor

cdef class PyFoo:
    cdef Foo *thisptr      # hold a C++ instance which we're wrapping

    def __cinit__( self, *args, **kwargs):
        # get list of arg types to distinquish …
Run Code Online (Sandbox Code Playgroud)

python constructor overriding cython python-2.7

5
推荐指数
1
解决办法
1735
查看次数

标签 统计

constructor ×1

cython ×1

overriding ×1

python ×1

python-2.7 ×1