Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/58.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 托卢亚的联合类型_C_Lua - Fatal编程技术网

C 托卢亚的联合类型

C 托卢亚的联合类型,c,lua,C,Lua,我认为托卢亚有一个关于工会的错误。如果您有这样的声明: struct SDL_WindowEvent { int type; int windowID; }; union SDL_Event { int type; SDL_WindowEvent window; }; extern SDL_Event * create(void); extern void frobnicate(SDL_Event *); event = create() frobnicat

我认为托卢亚有一个关于工会的错误。如果您有这样的声明:

struct SDL_WindowEvent {
    int type;
    int windowID;
};

union SDL_Event {
    int type;
    SDL_WindowEvent window;
};

extern SDL_Event * create(void);
extern void frobnicate(SDL_Event *);
event = create()
frobnicate(event)
print(event.window.windowId)
frobnicate(event)
那么lua代码应该是这样的:

struct SDL_WindowEvent {
    int type;
    int windowID;
};

union SDL_Event {
    int type;
    SDL_WindowEvent window;
};

extern SDL_Event * create(void);
extern void frobnicate(SDL_Event *);
event = create()
frobnicate(event)
print(event.window.windowId)
frobnicate(event)
但对Frobnite(事件)的第二次调用将失败,并出现错误:

argument #1 is 'SDL_WindowEvent'; 'SDL_Event' expected.
稍微戳一下调试器就会发现event.windowaccess中的tolua_pushusertype重写了我的变量类型

以下是我迄今为止所尝试的:在我的声明中,tolua将创建以下用于声明SDL_WindowEvent类的调用:

tolua_cclass(tolua_S,"SDL_WindowEvent","SDL_WindowEvent","",NULL);
因此,使SDL_事件和SDL_WindowEvent成为两个不相关的类,而不是彼此的基类。假设我后面有代码,它等价于以下内容:

tolua_pushusertype(tolua_S, event, "SDL_Event");
assert(tolua_isusertype(tolua_S,1,"SDL_Event",0,&tolua_err));
tolua_pushusertype(tolua_S, event->window, "SDL_WindowEvent");
assert(tolua_isusertype(tolua_S,2,"SDL_Event",0,&tolua_err));
然后第四行中的断言将失败,因为堆栈上的值自第二行中的断言之后神奇地改变了类型。这是因为tolua_pushusertype()更改了它-foo和foo.window具有相同的地址,并且在内部,tolua每个地址只跟踪一种类型。如果对象是基类型SDL_Event,则tolua_isusertype()会得到满足,但这需要上面的tolua_cclass声明将“SDL_Event”作为其第四个参数。当我手动修复它时,两个断言都通过了,但我不能每次都手动更改它-我想修复tolua来完成这项工作,但我没有 要做到这一点,还需要足够的理解。我甚至不知道这样做是否正确

我使用的是tolua 5.1.4,但tolua++1.92.3也存在同样的问题


首先,我做错什么了吗?我有没有办法重写我的声明,让托卢亚的工作保持原样?或者,如果失败了,我可以向tolua申请一个解决方案,让它起作用吗?

我想出来了!解决方案是编辑我的.pkg文件,如下所示:

struct SDL_WindowEvent : SDL_Event {
    int type;
    int windowID;
};

这使得tolua的类层次结构显式。我太专注于.pkg内容是有效的C代码,这似乎是。

如果您找到了解决方案,请将问题标记为已解决。因此让我等了两天才允许接受我自己的答案。