从类型“MyStruct”到类型“void*”的强制转换无效

2024-10-05 14:29:55 发布

您现在位置:Python中文网/ 问答频道 /正文

我在下面的头文件中定义了一个名为Spiketimesolver的结构:

#include <math.h>
#include "plot_support.h"

struct Spiketimesolver {
  const Doub tau_0, I_0, V_start, I_start;
Spiketimesolver(const Doub tau_0, const Doub I_0, const Doub V_start,
                const Doub I_start) :
  tau_0(tau_0), I_0(I_0), V_start(V_start), I_start(I_start)
  {}
  Doub operator()(const Doub t) {
    const Doub I = get_I(I_start, t, tau_0);
    const Doub V = get_V(I_start, t, tau_0, I_0, V_start, I);
    return 0.5 * pow((V - 1), 2);
  }
};

get_Iget_V在我的文件plot_support.h中定义。它们的定义在这里无关紧要。)

在单独的模块中,我将此结构实例化如下:

^{pr2}$

在这里,所有的论点都是双重的。在

我想将solver传递给我定义的函数:

template<class T> PyObject* NRpyObject(T &a) {
// default applies to all function objects or other structs
    PyObject *thing = PyCapsule_New((void*)a,NULL,NULL);
    return thing;
}

^{}是pythoncapi的一部分。它期望void *pointer作为第一个参数。在

当我试图编译时,对于调用NRpyObject(solver),我得到以下错误:

In file included from analysis_cmodule.cpp:2:0:
nr3python.h: In instantiation of ‘PyObject* NRpyObject(T&) [with T = Spiketimesolver; PyObject = _object]’:
analysis_cmodule.cpp:16:27:   required from here
nr3python.h:183:52: error: invalid cast from type ‘Spiketimesolver’ to type ‘void*’
  PyObject *thing = PyCapsule_New((void*)a,NULL,NULL);
                                                    ^

我做错了什么?在


Tags: fromget定义include结构startnullpyobject