类型错误:无法访问带有双指针参数的包装函数
我对 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为你的问题实际上在于:
并且你的参数应该是 double,而不是 double*。
参考您的评论
您的代码是:
您的原型是:
“Hello”和“World”是衰减为 const char* 类型的字符串文字。所以,它们是指针。当您在 test_cc 中传递 c0 和 c1 时,它很好,因为它需要一个指针。
虽然 c0 是 test_cd 中的指针,但 d0 不是 - d0 是 double 类型,而不是指向 double 的指针。因此,您必须传递一个指向 double 的指针。
您可以通过在堆栈上创建一个 double 并传入其地址:
或声明一个指针并使其指向堆栈变量,并将其传入:
或声明一个指向动态内存的指针并将其传入:
不过如果可以的话我会避免第三个。
再次编辑...
http://www.dalkescientific.com /writings/NBN/c_extensions.html
显示了如何从 python 调用 C 代码(包括传递 double)的工作示例。在 python 和 C 代码中搜索 iterate_point 以了解其工作原理。你不应该有一个指向双精度的指针,它应该只是一个普通的双精度,如前所述。
I think your problem is actually in:
and that your argument should be a
double
, not adouble*
.In reference to your comment
Your code is:
Your prototypes are:
"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:
or by declaring a pointer and making it point to a stack variable, passing that in:
or by declaring a pointer pointing to dynamic memory and passing that in:
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.