类型错误:无法访问带有双指针参数的包装函数

发布于 2024-12-17 06:31:06 字数 1313 浏览 0 评论 0原文

我对 swig 很陌生,当我访问一个以双指针作为参数的函数时,遇到了一个奇怪的 TypeError 。

以下是我的文件:

Example.hpp、

#ifndef _EXAMPLE_HPP
#define _EXAMPLE_HPP

class Example {
public:
    void test_cc(char * c0, char * c1);
    void test_cd(char * c0, double d0);
    void test_cdp(char * c0, double * d0);
};

#endif // _EXAMPLE_HPP

Example.cpp

#include "Example.hpp"

void Example::test_cc(char * c0, char * c1) {}
void Example::test_cd(char * c0, double d0) {}
void Example::test_cdp(char * c0, double * d0) {}

、Example.i

%module Example
%{
#include "Example.hpp"
%}

#include "Example.hpp"

,最后是我的测试文件 test_Example.py:

#!/usr/bin/env python

import Example

E = Example.Example()

E.test_cc("Hello","World");
E.test_cd("Hello",42);
E.test_cdp("Hello",42);

当我运行 ./test_Example.py 时,我收到错误消息

Traceback (most recent call last):
  File "./test_Example.py", line 9, in <module>
    E.test_cdp("Hello",42);
  File "Example.py", line 77, in test_cdp
    def test_cdp(self, *args): return _Example.Example_test_cdp(self, *args)
TypeError: in method 'Example_test_cdp', argument 3 of type 'double *'

访问函数 test_cc 有效,test_cd 也...为什么不 test_cdp ?

我的错误在哪里?

Im very new to swig and got a strange TypeError when i access a function with an double pointer as argument.

Here are my files:

Example.hpp

#ifndef _EXAMPLE_HPP
#define _EXAMPLE_HPP

class Example {
public:
    void test_cc(char * c0, char * c1);
    void test_cd(char * c0, double d0);
    void test_cdp(char * c0, double * d0);
};

#endif // _EXAMPLE_HPP

Example.cpp

#include "Example.hpp"

void Example::test_cc(char * c0, char * c1) {}
void Example::test_cd(char * c0, double d0) {}
void Example::test_cdp(char * c0, double * d0) {}

Example.i

%module Example
%{
#include "Example.hpp"
%}

#include "Example.hpp"

and finally my testfile test_Example.py:

#!/usr/bin/env python

import Example

E = Example.Example()

E.test_cc("Hello","World");
E.test_cd("Hello",42);
E.test_cdp("Hello",42);

When i run ./test_Example.py, i get the Error Message

Traceback (most recent call last):
  File "./test_Example.py", line 9, in <module>
    E.test_cdp("Hello",42);
  File "Example.py", line 77, in test_cdp
    def test_cdp(self, *args): return _Example.Example_test_cdp(self, *args)
TypeError: in method 'Example_test_cdp', argument 3 of type 'double *'

Accessing the function test_cc works, test_cd also ... why not test_cdp?

Where is my mistake?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

非要怀念 2024-12-24 06:31:06

我认为你的问题实际上在于:

void test_cd(char * c0, double * d0);

并且你的参数应该是 double,而不是 double*。

参考您的评论

您的代码是:

E.test_cc("Hello","World");
E.test_cd("Hello",42);

您的原型是:

void test_cc(char * c0, char * c1);
void test_cd(char * c0, double d0);

“Hello”和“World”是衰减为 const char* 类型的字符串文字。所以,它们是指针。当您在 test_cc 中传递 c0 和 c1 时,它很好,因为它需要一个指针。

虽然 c0 是 test_cd 中的指针,但 d0 不是 - d0 是 double 类型,而不是指向 double 的指针。因此,您必须传递一个指向 double 的指针。

您可以通过在堆栈上创建一个 double 并传入其地址:

double hi_mom = 83.2;
test_cd("some string" &hi_mom);

或声明一个指针并使其指向堆栈变量,并将其传入:

double hi_mom = 83.2;
double* ptr = &hi_mom;
test_cd("some string", ptr);

或声明一个指向动态内存的指针并将其传入:

double* ptr = new double(83.2);
test_cd("some string", ptr);
delete ptr;

不过如果可以的话我会避免第三个。

再次编辑...

http://www.dalkescientific.com /writings/NBN/c_extensions.html

显示了如何从 python 调用 C 代码(包括传递 double)的工作示例。在 python 和 C 代码中搜索 iterate_point 以了解其工作原理。你不应该有一个指向双精度的指针,它应该只是一个普通的双精度,如前所述。

I think your problem is actually in:

void test_cd(char * c0, double * d0);

and that your argument should be a double, not a double*.

In reference to your comment

Your code is:

E.test_cc("Hello","World");
E.test_cd("Hello",42);

Your prototypes are:

void test_cc(char * c0, char * c1);
void test_cd(char * c0, double d0);

"Hello" and "World" are string literals that decay to type const char*. So, they are pointers. When you pass c0 and c1 in test_cc its fine because it expects a pointer.

while c0 is a pointer in test_cd, d0 is not - d0 is of type double, NOT pointer-to-double. So, you must pass in a pointer to a double instead.

You can do this by either creating a double on the stack and passing in its address:

double hi_mom = 83.2;
test_cd("some string" &hi_mom);

or by declaring a pointer and making it point to a stack variable, passing that in:

double hi_mom = 83.2;
double* ptr = &hi_mom;
test_cd("some string", ptr);

or by declaring a pointer pointing to dynamic memory and passing that in:

double* ptr = new double(83.2);
test_cd("some string", ptr);
delete ptr;

Though I'd avoid the 3rd one if you can.

Edit again...

http://www.dalkescientific.com/writings/NBN/c_extensions.html

shows a working example of how to call C code from python including passing a double. Search for iterate_point in both the python and C code to see how it works. You shouldn't have a pointer for the double, it should just be a normal double as said earlier.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文