如何从SIP到C++结构到Python中的一个结构?

2024-10-01 07:15:40 发布

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

<>我回答了{{a1}),解释如何将C++从Python转换为Python(联盟不被自动支持)。

当我对现有结构中的联合使用这种类型的包装时,当我尝试使用sip安装进行构建时(我将“包装”重命名为“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;
};

有人知道如何解决这个问题吗

提前谢谢你

约翰尼


Tags: valuetagtypestructintenddoubleunion