我在 DLL 中将一些类导出到 Luabind,这 2 个类(LuaScriptManager、EventManager)一切正常。 我可以从 Lua 调用它们的函数,一切正常,但现在我正尝试在我的客户端可执行文件中设置一些新类,该类与 DLL 链接,但到目前为止一点运气都没有。
这是我调用的每个函数得到的错误消息: “未找到匹配的重载,候选者:void loadResource(ResourceManager&, std::string const&)”
类绑定(bind)来自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)
这种奇怪行为的原因可能是什么?
最佳答案
这是我发送到 Luabind 邮件列表的邮件的拷贝。 http://sourceforge.net/mailarchive/message.php?msg_id=27420879
我不确定这是否也适用于 Windows 和 DLL,但我有一个类似的 在 Linux 上使用 GCC 和共享模块的经验:已注册的类 使用 Luabind 仅在该共享库中有效,但导致 如果跨共享库边界使用,则会出现段错误。
解决方案是修补 luabind::type_id 类,并比较 使用 typeid(T).name() 而不是 typeid(T)::operator=。对于海湾合作委员会, 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
关于c++ - 从 Luabind 调用 C++ 成员函数导致 "No matching overload found",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5798938/