lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


I've just recently picked up Lua and have been exploring its potential
as a possible game scripting engine for any future projects. For better
integration with C++ I've chosen to play around with Luabind. So far,
I've been quite impressed with its capabilities and ease of use. I have,
however, run into a seemingly minor problem that I cannot, for the life
of me, figure out.

I've been writing a small test program from pieces of this article:
http://www.nuclex.org/articles/quick-introduction-to-luabind , and
successfully bound my own class to Lua and passed its objects back and
forth. Then I wanted to try and bind std::vector, as I'll undoubtedly
want to send vectors of game objects back and forth for Lua to
manipulate. I bound vector<int> and the push_back() method to start off,
and this worked fine. The problem arose when trying to add the at()
method. For some reason, this won't compile, and I cannot understand
why. At first I thought it was perhaps because at() returns a templated
value, but then I've already specified that I want the templated type to
be an int. I must be doing something wrong where I supply the address of the function. If the line that binds at() is commented out, the program runs perfectly, and the call to makeivector() will indeed produce a vector<int> with the values 3 and 13 within.

My code and the compile errors I get are provided below. I can't seem to find any resources that indicate that others have had this problem, so I must be doing something wrong. Unfortunately, I also can't find a sample program on the web that binds std::vector methods either. Any help would be much appreciated. (It's been hard to find a good group of Lua people out there on the internet!)

Here's my code:

#include <iostream>
#include <string>
#include <vector>

extern "C"
{
   #include "lua/lua.h"
   #include "lua/lauxlib.h"
}

#include "luabind/luabind.hpp"

int main()
{

   lua_State *state = lua_open();

   luabind::open(state);

   luabind::module(state)
   [
      luabind::class_<std::vector<int>>("ivector")
         .def(luabind::constructor<>())
         .def("push_back", &std::vector<int>::push_back)
         .def("at", &std::vector<int>::at)
   ];

   luaL_dostring(state,
      "function makeivector()\n"
      "  v = ivector()\n"
      "  v:push_back(3)\n"
      "  v:push_back(13)\n"
      "  return v\n"
      "end\n"
   );

   std::vector<int> iv = luabind::call_function<std::vector<int>>(state,
"makeivector");
   for (std::vector<int>::iterator i = iv.begin(); i != iv.end(); ++i)
   {
      std::cout << *i << std::endl;
   }

   lua_close(state);

}

And here are the compile errors I get:

1>------ Build started: Project: luabind, Configuration: Debug Win32 ------
1>Compiling...
1>main.cpp
1>e:\my documents\my c++\tests\luabind\luabind\main.cpp(69) : error
C2065: 'state' : undeclared identifier
1>e:\my documents\my c++\tests\luabind\luabind\main.cpp(84) : error
C2780: 'luabind::class_<T>
&luabind::class_<T>::def(luabind::detail::operator_<Derived>)' : expects
1 arguments - 2 provided
1>        with
1>        [
1>            T=std::vector<int>
1>        ]
1>        c:\program files\microsoft visual studio
8\vc\include\luabind\class.hpp(1032) : see declaration of
'luabind::class_<T>::def'
1>        with
1>        [
1>            T=std::vector<int>
1>        ]
1>e:\my documents\my c++\tests\luabind\luabind\main.cpp(84) : error
C2784: 'luabind::class_<T>
&luabind::class_<T>::def(luabind::detail::operator_<Derived>,const
Policies &)' : could not deduce template argument for
'luabind::detail::operator_<Derived>' from 'const char [3]'
1>        with
1>        [
1>            T=std::vector<int>
1>        ]
1>        c:\program files\microsoft visual studio
8\vc\include\luabind\class.hpp(1022) : see declaration of
'luabind::class_<T>::def'
1>        with
1>        [
1>            T=std::vector<int>
1>        ]
1>e:\my documents\my c++\tests\luabind\luabind\main.cpp(84) : error
C2784: 'luabind::class_<T>
&luabind::class_<T>::def(luabind::constructor<A0,A1,A2,A3,A4,A5,A6,A7,A8,A9>,const
Policies &)' : could not deduce template argument for
'luabind::constructor<A0,A1,A2,A3,A4,A5,A6,A7,A8,A9>' from 'const char [3]'
1>        with
1>        [
1>            T=std::vector<int>
1>        ]
1>        c:\program files\microsoft visual studio
8\vc\include\luabind\class.hpp(900) : see declaration of
'luabind::class_<T>::def'
1>        with
1>        [
1>            T=std::vector<int>
1>        ]
1>e:\my documents\my c++\tests\luabind\luabind\main.cpp(84) : error
C2780: 'luabind::class_<T>
&luabind::class_<T>::def(luabind::constructor<A0,A1,A2,A3,A4,A5,A6,A7,A8,A9>)'
: expects 1 arguments - 2 provided
1>        with
1>        [
1>            T=std::vector<int>
1>        ]
1>        c:\program files\microsoft visual studio
8\vc\include\luabind\class.hpp(890) : see declaration of
'luabind::class_<T>::def'
1>        with
1>        [
1>            T=std::vector<int>
1>        ]
1>e:\my documents\my c++\tests\luabind\luabind\main.cpp(84) : error
C2780: 'luabind::class_<T> &luabind::class_<T>::def(const char
*,F,Default,const Policies &)' : expects 4 arguments - 2 provided
1>        with
1>        [
1>            T=std::vector<int>
1>        ]
1>        c:\program files\microsoft visual studio
8\vc\include\luabind\class.hpp(881) : see declaration of
'luabind::class_<T>::def'
1>        with
1>        [
1>            T=std::vector<int>
1>        ]
1>e:\my documents\my c++\tests\luabind\luabind\main.cpp(84) : error
C2780: 'luabind::class_<T> &luabind::class_<T>::def(const char
*,F,DefaultOrPolicies)' : expects 3 arguments - 2 provided
1>        with
1>        [
1>            T=std::vector<int>
1>        ]
1>        c:\program files\microsoft visual studio
8\vc\include\luabind\class.hpp(873) : see declaration of
'luabind::class_<T>::def'
1>        with
1>        [
1>            T=std::vector<int>
1>        ]
1>e:\my documents\my c++\tests\luabind\luabind\main.cpp(84) : error
C2914: 'luabind::class_<T>::def' : cannot deduce template argument as
function argument is ambiguous
1>        with
1>        [
1>            T=std::vector<int>
1>        ]
1>e:\my documents\my c++\tests\luabind\luabind\main.cpp(84) : error
C2784: 'luabind::class_<T> &luabind::class_<T>::def(const char *,F)' :
could not deduce template argument for 'overloaded function type' from
'overloaded function type'
1>        with
1>        [
1>            T=std::vector<int>
1>        ]
1>        c:\program files\microsoft visual studio
8\vc\include\luabind\class.hpp(864) : see declaration of
'luabind::class_<T>::def'
1>        with
1>        [
1>            T=std::vector<int>
1>        ]
1>Build log was saved at "file://e:\My Documents\My
C++\tests\luabind\luabind\Debug\BuildLog.htm"
1>luabind - 9 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========