从 Luabind 调用 C++ 成员函数会导致 "没有找到匹配的重载" 错误。
2011-4-27 3:15:19
收藏:0
阅读:165
评论:1
我已经将一些类导出到 Luabind 的 DLL 中,并且这些类(LuaScriptManager,EventManager)的一切都运作良好。我可以从 Lua 调用它们的函数,一切都很好,但是现在我正在尝试在我的客户端可执行文件中设置一些新类,这些新类与 DLL 链接在一起,到目前为止还没有成功。每次调用函数时,我都会收到以下错误消息:"No matching overload found, candidates: void loadResource(ResourceManager&, std::string const&)"。 类绑定来自于 http://www.nuclex.org/articles/5-cxx/1-quick-introduction-to-luabind
代码如下:
struct Manager {
Manager() :
m_ResourceCount(0) {}
void loadResource(const std::string &sFilename) {
++m_ResourceCount;
}
size_t getResourceCount() const {
return m_ResourceCount;
}
size_t m_ResourceCount;
};
static Manager MyResourceManager;
void Bind(lua_State* l)
{
// Export our class with LuaBind
luabind::module(l) [
luabind::class_<Manager>("ResourceManager")
.def("loadResource", &Manager::loadResource)
.property("ResourceCount", &Manager::getResourceCount)
];
luabind::globals(l)["MyResourceManager"] = &MyResourceManager;
}
相应的 Lua 测试代码:
-- The following call will fail with the above error
MyResourceManager:loadResource("abc.res")
--MyResourceManager:loadResource("xyz.res")
-- If the function is commented out, this will work (accessing the property)
ResourceCount = MyResourceManager.ResourceCount
-- Calling my other classes' functions work fine
LuaScriptManager.GetInstance():WriteLine(ResourceCount)
这种奇怪行为的原因是什么呢?
原文链接 https://stackoverflow.com/questions/5798938
点赞
评论区的留言会收到邮件通知哦~
推荐文章
- 如何在roblox studio中1:1导入真实世界的地形?
- 求解,lua_resume的第二次调用继续执行协程问题。
- 【上海普陀区】内向猫网络招募【Skynet游戏框架Lua后端程序员】
- SF爱好求教:如何用lua实现游戏内调用数据库函数实现账号密码注册?
- Lua实现网站后台开发
- LUA错误显式返回,社区常见的规约是怎么样的
- lua5.3下载库失败
- 请问如何实现文本框内容和某个网页搜索框内容连接,并把网页输出来的结果反馈到另外一个文本框上
- lua lanes多线程使用
- 一个kv数据库
- openresty 有没有比较轻量的 docker 镜像
- 想问一下,有大佬用过luacurl吗
- 在Lua执行过程中使用Load函数出现问题
- 为什么 neovim 里没有显示一些特殊字符?
- Lua比较两个表的值(不考虑键的顺序)
- 有个lua简单的项目,外包,有意者加微信 liuheng600456详谈,最好在成都
- 如何在 Visual Studio 2022 中运行 Lua 代码?
- addEventListener 返回 nil Lua
- Lua中获取用户配置主目录的跨平台方法
- 如何编写 Lua 模式将字符串(嵌套数组)转换为真正的数组?
这是我发送给 Luabind 邮件列表的一份复制。 http://sourceforge.net/mailarchive/message.php?msg_id=27420879
我不确定这是否也适用于 Windows 和 DLL,但在 Linux 上使用 GCC 和共享模块时,我有过类似的经历:使用 Luabind 注册的类只能在该共享库中有效,但在共享库边界上使用会导致分段错误。
解决方案是对 luabind::type_id 类进行修补,使用 typeid(T).name() 进行比较,而不是 typeid(T)::operator=。对于 GCC,typeid 运算符可能无法在共享库之间工作的原因在这里解释了 [1]。在这种特殊情况下,我使用了带有 Lua 的 require() 的共享库加载,不幸的是它没有将 RTLD_GLOBAL 传递给 dlopen。
[1] http://gcc.gnu.org/faq.html#dso
typeid 相等性问题已经出现在其他 C++ 库中,例如 boost::any [2],使用同样的修复方法 [3],即 typeid(T).name() 比较。
[2] https://svn.boost.org/trac/boost/ticket/754 [3] https://svn.boost.org/trac/boost/changeset/56168
也许附加的补丁可对 DLL 产生帮助。
--- include.orig/luabind/typeid.hpp +++ include/luabind/typeid.hpp @@ -6,6 +6,7 @@ # define LUABIND_TYPEID_081227_HPP # include <boost/operators.hpp> +# include <cstring> # include <typeinfo> # include <luabind/detail/primitives.hpp> @@ -33,17 +34,17 @@ bool operator!=(type_id const& other) const { - return *id != *other.id; + return std::strcmp(id->name(), other.id->name()) != 0; } bool operator==(type_id const& other) const { - return *id == *other.id; + return std::strcmp(id->name(), other.id->name()) == 0; } bool operator<(type_id const& other) const { - return id->before(*other.id); + return std::strcmp(id->name(), other.id->name()) < 0; } char const* name() const