SWIG不能转换typedef类型正确 [英] SWIG cannot convert typedef type correct

查看:1115
本文介绍了SWIG不能转换typedef类型正确的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用SWIT将vc项目转换为python。
当结构有一个成员,类型是typedef char TEXT [16]无法正确转换时,我发现。
例如:

I'm using SWIT to convert a vc project to python. I found when a struct has a member which type is like "typedef char TEXT[16]" cannot be converted correctly. for example:

typedef char TEXT[16];
struct MYSTRUCT
{       
    TEXT    TradingDay;     
};

包装cpp无法编译。
错误C2075:'操作符new()的目标:数组初始化需要花括号
BUT,如果typedef不是数组,如下:

The wrapper cpp cannot compile all right. "error C2075: 'Target of operator new()' : array initialization needs curly braces" BUT,if typedef is not an array , like this:

    typedef int NUMBER;
    struct MYSTRUCT2
{       
    NUMBER Money;       
};

会没事的。
我该怎么办?
thx!

there will be all right. what should I do? thx!

PS:
i档案:

P.S: i file:

%module MyDataAPI
%include "typemaps.i"

%header %{
#include "../References/MyDataAPI.h"

%}

namespace MyDataAPI
{
     struct MYSTRUCT
    {       
        TEXT    TradingDay;     
    };
    struct MYSTRUCT2
    {       
        NUMBER Money;       
    };
}


推荐答案

$ c> typedef 语句由SWIG处理。 %header 仅向生成的文件添加代码,该数据不由SWIG处理。 %inline 都将代码直接添加到生成的文件中,并且使用SWIG处理它。这是我的 .i 文件:

Make sure your typedef statements are processed by SWIG. %header only adds code to the generated file, that data is not processed by SWIG. %inline both adds the code directly to the generated file and processes it with SWIG. Here's my .i file:

%module x

%inline %{
    typedef char TEXT[16];
    typedef int NUMBER;
    namespace MyDataAPI
    {
        struct MYSTRUCT
        {
            TEXT TradingDay;
        };
        struct MYSTRUCT2
        {
            NUMBER Money;
        };
    }
%}

并使用:

T:\>py
Python 3.3.0 (v3.3.0:bd8afb90ebf2, Sep 29 2012, 10:57:17) [MSC v.1600 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import x
>>> a=x.MYSTRUCT()
>>> a.TradingDay
''
>>> a.TradingDay='ABCDEFGHIJKLMNOPQ'   # Note this is too long, 17 chars...
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: in method 'MYSTRUCT_TradingDay_set', argument 2 of type 'char [16]'
>>> a.TradingDay='ABCDEFGHIJKLMNOP'
>>> a.TradingDay
'ABCDEFGHIJKLMNOP'
>>> b=x.MYSTRUCT2()
>>> b.Money
0
>>> b.Money=100
>>> b.Money
100

这篇关于SWIG不能转换typedef类型正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆