【发布时间】:2020-12-09 17:04:47
【问题描述】:
我回答了this question,解释了如何使用 SIP 将联合从 c++ 转换为 python(不自动支持联合)。
当我将这种类型的包装用于现有结构中的联合时,当我尝试使用 sip-install 构建时收到以下错误消息(我将 'wrapped_u' 重命名为 'u' 并将 'test_u' 重命名为 'type_u' ):
error: field 'u' has incomplete type 'type_u'
和
note: forward declaration of ‘union type_u’.
这里是c++头文件:
struct myType{
enum {CHAR, INT, DOUBLE} tag;
union type_u{
/* tag = CHAR */
char char_value;
/* tag = INT */
int int_value;
/* tag = DOUBLE */
double double_value;
};
type_u u;
void print_my_type() const;
};
这是 SIP 文件:
struct myType{
enum tag {CHAR, INT, DOUBLE};
struct union_wrapper /PyName=type_u/
{
%TypeHeaderCode
#include <test_struct_union.h>
struct union_wrapper
{
union type_u u;
};
%End
// tag = CHAR
char char_value {
%GetCode
sipPy = PyUnicode_FromString(&(sipCpp->u.char_value));
%End
%SetCode
if (PyUnicode_Check(sipPy))
sipCpp->u.char_value;
else
sipErr = 1;
%End
};
// tag = INT
int int_value {
%GetCode
sipPy = PyLong_FromLong(sipCpp->u.int_value);
%End
%SetCode
if (PyLong_Check(sipPy))
sipCpp->u.int_value;
else
sipErr = 1;
%End
};
// tag = DOUBLE
double double_value {
%GetCode
sipPy = PyFloat_FromDouble(sipCpp->u.double_value);
%End
%SetCode
if (PyFloat_Check(sipPy))
sipCpp->u.double_value;
else
sipErr = 1;
%End
};
};
void print_my_type() const;
};
有人知道如何解决这个问题吗?
提前谢谢你!
强尼
【问题讨论】:
标签: python c++ wrapper python-sip