使用luabind和std::vector ::at返回'Trying to use unregistered class'。

我正在尝试使用 luabind 和 std::vector。我使用带有 c++11 的 gcc 4.8.1。转换代码如下所示

template <typename T>
void luabindVector(lua_State* S, std::string tname) {
module(S)[class_<std::vector<T> >(tname.c_str()).def(constructor<>()).def(
        "pushBack",
        static_cast<void (std::vector<T>::*)(
                const T&)>(&std::vector<T>::push_back)).
                def("at",(T& (std::vector<T>::*)(size_t))(&std::vector<T>::at)).
                def("resize",static_cast<void (std::vector<T>::*)(size_t)>(&std::vector<T>::resize)).
                def("size",&std::vector<T>::size).
                def("assign",static_cast<void (std::vector<T>::*)(size_t, const T&)>(&std::vector<T>::assign))];
}

使用以下命令

luabindVector<double>(L,"vectorDouble");

在 Lua 中,我有以下命令

tans = vectorDouble()
tans:resize(3)
a=tans:at(1)

这会产生错误 std::runtime_error: 'Trying to use unregistered class',并显示错误 2。

请注意,push_back、resize、size 和 assign 都正常工作,但程序在调用 tans:at(1) 时失败。从文档中并不清楚我应该如何正确操作。有人知道我做错了什么吗?

点赞