簡體   English   中英

Lua與Luabind編譯錯誤C ++

[英]Lua with Luabind compilation error c++

我無法正常工作。

這是我的代碼,我將其保持盡可能簡單:

#include <iostream>
#include <lua/lua.hpp>
#include <luabind/luabind.hpp>
#include <luabind/class.hpp>

class           MyClass
{
public:
  MyClass() {}
  ~MyClass() {}
  void          myFunc() { std::cout << "Hi !" << std::endl; }
  int           getMyVar2() { return (this->myVar2); }
  int           myVar;
private:
  int           myVar2;
};

int             main()
{
  MyClass       *m = new MyClass();
  lua_State     *L = luaL_newstate();

  luabind::open(L);
  luabind::module(L)
    [
     luabind::class_<MyClass>("MyClass")
     .def("myFunc", &MyClass::myFunc)
     .def("getMyVar2", &MyClass::getMyVar2)
     .def("myVar", &MyClass::myVar)
     ];
  luabind::globals(L)["globalName"] = m;

  if (luaL_dofile(L, "example.lua"))
    fprintf(stderr, "%s\n", lua_tostring(L, -1));
  lua_close(L);
}

編譯器給了我很多垃圾信息,並且通過重定向輸出,我首先看到:

/usr/local/include/luabind/class.hpp: In instantiation of ‘void luabind::detail::memfun_registration<Class, F, Policies>::register_(lua_State*) const [with Class = MyClass; F = int MyClass::*; Policies = luabind::detail::null_type; lua_State = lua_State]’:
exemple_luabind.cpp:48:1:   required from here
/usr/local/include/luabind/class.hpp:311:52: error: no matching function for call to ‘deduce_signature(int MyClass::* const&, MyClass*)’

第48行是:

}

有人可以幫我嗎? 我確定這是一個初學者的錯誤。

您不能使用.def聲明屬性,因為它僅用於函數。 根據您要訪問類成員的方式,可以使用不同的方法:

luabind::module(L)
        [
            luabind::class_<MyClass>("MyClass")
            .def("myFunc", &MyClass::myFunc)
            .def("getMyVar2", &MyClass::getMyVar2) // function with MyClass:getMyVar2()
            .property("myVar2", &MyClass::getMyVar2) // MyClass.myVar2 is only readable, same as MyClass:getMyVar2()
            .property("myVar", &MyClass::myVar) // MyClass.myVar is only readable
            // vs
            .def_readwrite("myVarT", &MyClass::myVar) // MyClass.myVarT is read and writable
        ];
    luabind::globals(L)["globalName"] = m;

檢查doku以獲得詳細說明: LuaBind文檔

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM