1. <i id='DSDD1'><tr id='DSDD1'><dt id='DSDD1'><q id='DSDD1'><span id='DSDD1'><b id='DSDD1'><form id='DSDD1'><ins id='DSDD1'></ins><ul id='DSDD1'></ul><sub id='DSDD1'></sub></form><legend id='DSDD1'></legend><bdo id='DSDD1'><pre id='DSDD1'><center id='DSDD1'></center></pre></bdo></b><th id='DSDD1'></th></span></q></dt></tr></i><div id='DSDD1'><tfoot id='DSDD1'></tfoot><dl id='DSDD1'><fieldset id='DSDD1'></fieldset></dl></div>
      <bdo id='DSDD1'></bdo><ul id='DSDD1'></ul>

    <small id='DSDD1'></small><noframes id='DSDD1'>

      <tfoot id='DSDD1'></tfoot>
    1. <legend id='DSDD1'><style id='DSDD1'><dir id='DSDD1'><q id='DSDD1'></q></dir></style></legend>

      在 Lua 中注册 C++ 函数?

      时间:2023-09-27

        <tbody id='FP2g0'></tbody>

        <bdo id='FP2g0'></bdo><ul id='FP2g0'></ul>

        <small id='FP2g0'></small><noframes id='FP2g0'>

        <i id='FP2g0'><tr id='FP2g0'><dt id='FP2g0'><q id='FP2g0'><span id='FP2g0'><b id='FP2g0'><form id='FP2g0'><ins id='FP2g0'></ins><ul id='FP2g0'></ul><sub id='FP2g0'></sub></form><legend id='FP2g0'></legend><bdo id='FP2g0'><pre id='FP2g0'><center id='FP2g0'></center></pre></bdo></b><th id='FP2g0'></th></span></q></dt></tr></i><div id='FP2g0'><tfoot id='FP2g0'></tfoot><dl id='FP2g0'><fieldset id='FP2g0'></fieldset></dl></div>
            <tfoot id='FP2g0'></tfoot>
            <legend id='FP2g0'><style id='FP2g0'><dir id='FP2g0'><q id='FP2g0'></q></dir></style></legend>
                本文介绍了在 Lua 中注册 C++ 函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我正在尝试在 Lua 中注册一个 C++ 函数.

                I am trying to register a c++ function in Lua.

                但收到此错误:

                CScript.cpp|39|error: argument of type 'int (CScript::)(lua_State*)' does not match 'int (*)(lua_State*)'|
                

                int CApp::SetDisplayMode(int Width, int Height, int Depth)
                {
                    this->Screen_Width = Width;
                    this->Screen_Height = Height;
                    this->Screen_Depth = Depth;
                
                    return 0;
                }
                
                int CScript::Lua_SetDisplayMode(lua_State* L)
                {
                  // We need at least one parameter
                  int n = lua_gettop(L);
                  if(n < 0)
                  {
                    lua_pushstring(L, "Not enough parameter.");
                    lua_error(L);
                  }
                
                  int width = lua_tointeger(L, 1);
                  int height = lua_tointeger(L, 2);
                  int depth = lua_tointeger(L, 3);
                
                  lua_pushinteger(L, App->SetDisplayMode(width, height, depth));
                
                  return 0;
                }
                

                在主要部分:

                lua_register(L, "setDisplayMode", Lua_SetDisplayMode);
                

                推荐答案

                您不能直接使用基本的 Lua C API 在 Lua 中注册 C++ 非静态成员函数.

                You cannot directly register a C++ non-static member function in Lua using just the basic Lua C API.

                然而,任何可以轻松将 C++ 代码与 Lua 相关联的各种机制都允许您这样做.toLua++、SWIG、Luabind 等.如果你是认真的在 Lua 中使用 C++ 对象,我建议选择其中之一并使用它,而不是编写自己的版本.我个人使用 Luabind(大部分时间;SWIG 在工具箱中占有一席之地),因为它没有某种形式的代码生成.这一切完全是在 C++ 中完成的,因此没有生成 C++ 源文件的预通过步骤.

                However, any of the various mechanisms that exist for easily associating C++ code with Lua will allow you to do so. toLua++, SWIG, Luabind, etc. If you're serious about using C++ objects with Lua, I suggest picking one of those and using it, rather than writing your own version. I personally use Luabind (most of the time; SWIG has its place in the toolbox), as it is the one that doesn't have some form of code generation. It's all done purely in C++, so there's no pre-pass step that generates a C++ source file.

                这篇关于在 Lua 中注册 C++ 函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:在 C++ 中循环遍历所有 Lua 全局变量 下一篇:将 Lua 嵌入 C++

                相关文章

                  1. <legend id='2v3It'><style id='2v3It'><dir id='2v3It'><q id='2v3It'></q></dir></style></legend>

                    <small id='2v3It'></small><noframes id='2v3It'>

                    <i id='2v3It'><tr id='2v3It'><dt id='2v3It'><q id='2v3It'><span id='2v3It'><b id='2v3It'><form id='2v3It'><ins id='2v3It'></ins><ul id='2v3It'></ul><sub id='2v3It'></sub></form><legend id='2v3It'></legend><bdo id='2v3It'><pre id='2v3It'><center id='2v3It'></center></pre></bdo></b><th id='2v3It'></th></span></q></dt></tr></i><div id='2v3It'><tfoot id='2v3It'></tfoot><dl id='2v3It'><fieldset id='2v3It'></fieldset></dl></div>
                      <bdo id='2v3It'></bdo><ul id='2v3It'></ul>

                  2. <tfoot id='2v3It'></tfoot>