tostring

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

tostring

Ken Smith-2
I'm having a problem mapping an operator<< to tostring for my class.
My environment is GCC 4.0 on Mac OS X 10.4+.  Here is the code.

  1 #include <iostream>
  2
  3 #include "lua.hpp"
  4 #include "luabind/luabind.hpp"
  5
  6 struct test_t
  7 {
  8     test_t(const std::string& name)
  9         :
 10             name_(name)
 11     {
 12     }
 13     friend std::ostream& operator<<(
 14         std::ostream& os, const test_t& test
 15     );
 16
 17 private:
 18     std::string name_;
 19 };
 20
 21 std::ostream& operator<<(std::ostream& os, const test_t& test)
 22 {
 23     os
 24         << "test_t: "
 25         << test.name_;
 26
 27     return os;
 28 }
 29
 30 int main()
 31 {
 32     lua_State* L = luaL_newstate();
 33     luaL_openlibs(L);
 34     using namespace luabind;
 35     open(L);
 36     module(L)
 37     [
 38         class_<test_t>("test_t")
 39             .def(constructor<const std::string&>())
 40             .def(tostring(self))
 41     ];
 42     if (luaL_dostring(L, "t = test_t('hi') print(t)") == 1)
 43     {
 44         std::cout
 45             << "Couldn't run Lua program: "
 46             << lua_tostring(L, -1)
 47             << std::endl;
 48     }
 49
 50 }

Compiling this yields the following error.

src/main.cpp:40: error: 'self' was not declared in this scope
src/main.cpp:40: error: 'tostring' was not declared in this scope

Commenting out line 40 causes the sample to compile but of course, the
call to print(t) fails with:

Couldn't run Lua program: No such operator defined

How should I change this example to map test_t's operator<< to its
__tostring metamethod in Lua?

   Regards,
   Ken

------------------------------------------------------------------------------
This SF.net email is sponsored by Sprint
What will you do first with EVO, the first 4G phone?
Visit sprint.com/first -- http://p.sf.net/sfu/sprint-com-first
_______________________________________________
luabind-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user
Reply | Threaded
Open this post in threaded view
|

Re: tostring

Iliya Trendafilov
Hi, to bind operators you have to #include <luabind/operator.hpp>

On Thu, Jul 8, 2010 at 1:41 AM, Ken Smith <[hidden email]> wrote:
I'm having a problem mapping an operator<< to tostring for my class.
My environment is GCC 4.0 on Mac OS X 10.4+.  Here is the code.

 1 #include <iostream>
 2
 3 #include "lua.hpp"
 4 #include "luabind/luabind.hpp"
 5
 6 struct test_t
 7 {
 8     test_t(const std::string& name)
 9         :
 10             name_(name)
 11     {
 12     }
 13     friend std::ostream& operator<<(
 14         std::ostream& os, const test_t& test
 15     );
 16
 17 private:
 18     std::string name_;
 19 };
 20
 21 std::ostream& operator<<(std::ostream& os, const test_t& test)
 22 {
 23     os
 24         << "test_t: "
 25         << test.name_;
 26
 27     return os;
 28 }
 29
 30 int main()
 31 {
 32     lua_State* L = luaL_newstate();
 33     luaL_openlibs(L);
 34     using namespace luabind;
 35     open(L);
 36     module(L)
 37     [
 38         class_<test_t>("test_t")
 39             .def(constructor<const std::string&>())
 40             .def(tostring(self))
 41     ];
 42     if (luaL_dostring(L, "t = test_t('hi') print(t)") == 1)
 43     {
 44         std::cout
 45             << "Couldn't run Lua program: "
 46             << lua_tostring(L, -1)
 47             << std::endl;
 48     }
 49
 50 }

Compiling this yields the following error.

src/main.cpp:40: error: 'self' was not declared in this scope
src/main.cpp:40: error: 'tostring' was not declared in this scope

Commenting out line 40 causes the sample to compile but of course, the
call to print(t) fails with:

Couldn't run Lua program: No such operator defined

How should I change this example to map test_t's operator<< to its
__tostring metamethod in Lua?

  Regards,
  Ken

------------------------------------------------------------------------------
This SF.net email is sponsored by Sprint
What will you do first with EVO, the first 4G phone?
Visit sprint.com/first -- http://p.sf.net/sfu/sprint-com-first
_______________________________________________
luabind-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user


------------------------------------------------------------------------------
This SF.net email is sponsored by Sprint
What will you do first with EVO, the first 4G phone?
Visit sprint.com/first -- http://p.sf.net/sfu/sprint-com-first
_______________________________________________
luabind-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user
Reply | Threaded
Open this post in threaded view
|

Re: tostring

Ken Smith-2
Thank you, Iliya.

   Ken

On Fri, Jul 16, 2010 at 1:11 AM, Iliya Trendafilov <[hidden email]> wrote:

> Hi, to bind operators you have to #include <luabind/operator.hpp>
>
> On Thu, Jul 8, 2010 at 1:41 AM, Ken Smith <[hidden email]> wrote:
>>
>> I'm having a problem mapping an operator<< to tostring for my class.
>> My environment is GCC 4.0 on Mac OS X 10.4+.  Here is the code.
>>
>>  1 #include <iostream>
>>  2
>>  3 #include "lua.hpp"
>>  4 #include "luabind/luabind.hpp"
>>  5
>>  6 struct test_t
>>  7 {
>>  8     test_t(const std::string& name)
>>  9         :
>>  10             name_(name)
>>  11     {
>>  12     }
>>  13     friend std::ostream& operator<<(
>>  14         std::ostream& os, const test_t& test
>>  15     );
>>  16
>>  17 private:
>>  18     std::string name_;
>>  19 };
>>  20
>>  21 std::ostream& operator<<(std::ostream& os, const test_t& test)
>>  22 {
>>  23     os
>>  24         << "test_t: "
>>  25         << test.name_;
>>  26
>>  27     return os;
>>  28 }
>>  29
>>  30 int main()
>>  31 {
>>  32     lua_State* L = luaL_newstate();
>>  33     luaL_openlibs(L);
>>  34     using namespace luabind;
>>  35     open(L);
>>  36     module(L)
>>  37     [
>>  38         class_<test_t>("test_t")
>>  39             .def(constructor<const std::string&>())
>>  40             .def(tostring(self))
>>  41     ];
>>  42     if (luaL_dostring(L, "t = test_t('hi') print(t)") == 1)
>>  43     {
>>  44         std::cout
>>  45             << "Couldn't run Lua program: "
>>  46             << lua_tostring(L, -1)
>>  47             << std::endl;
>>  48     }
>>  49
>>  50 }
>>
>> Compiling this yields the following error.
>>
>> src/main.cpp:40: error: 'self' was not declared in this scope
>> src/main.cpp:40: error: 'tostring' was not declared in this scope
>>
>> Commenting out line 40 causes the sample to compile but of course, the
>> call to print(t) fails with:
>>
>> Couldn't run Lua program: No such operator defined
>>
>> How should I change this example to map test_t's operator<< to its
>> __tostring metamethod in Lua?
>>
>>   Regards,
>>   Ken
>>
>>
>> ------------------------------------------------------------------------------
>> This SF.net email is sponsored by Sprint
>> What will you do first with EVO, the first 4G phone?
>> Visit sprint.com/first -- http://p.sf.net/sfu/sprint-com-first
>> _______________________________________________
>> luabind-user mailing list
>> [hidden email]
>> https://lists.sourceforge.net/lists/listinfo/luabind-user
>
>
> ------------------------------------------------------------------------------
> This SF.net email is sponsored by Sprint
> What will you do first with EVO, the first 4G phone?
> Visit sprint.com/first -- http://p.sf.net/sfu/sprint-com-first
> _______________________________________________
> luabind-user mailing list
> [hidden email]
> https://lists.sourceforge.net/lists/listinfo/luabind-user
>
>

------------------------------------------------------------------------------
This SF.net email is sponsored by Sprint
What will you do first with EVO, the first 4G phone?
Visit sprint.com/first -- http://p.sf.net/sfu/sprint-com-first
_______________________________________________
luabind-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user