tostring

classic Classic list List threaded Threaded
5 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

ps. Apologies if you receive two copies of this mail.  I was not
subscribed to the list when I sent it the first time.

------------------------------------------------------------------------------
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

Nicolas Goles-2
I think self is a cocoa keyword. Try to use " this" instead

Sent from my iPhone

On Jul 8, 2010, at 12:59, 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
>
> ps. Apologies if you receive two copies of this mail.  I was not
> subscribed to the list when I sent it the first time.
>
> ------------------------------------------------------------------------------
> 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
On Thu, Jul 8, 2010 at 10:37 AM, Nicolas Goles <[hidden email]> wrote:
> I think self is a cocoa keyword. Try to use " this" instead

Thanks for your suggestion.  This is a .cpp file compiled with g++ as
C++ (without -x objective-*) so self is not a keyword for this
compilation unit.  I've also tried not using the using directive and
prefixing all of Luabind's symbols with its namespace.  (This is the
way I prefer to program anyway.)

    luabind::open(state_);
    luabind::module(state_)
    [
        luabind::class_<test_t>("test_t")
            .def(luabind::constructor<const std::string&>())
            .def(luabind::tostring(luabind::self))
    ];

This yields the same error.

   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

Nigel Atkinson-2
In reply to this post by Ken Smith-2
Hi Ken,

Add #include "luabind/operator.hpp"

after you include the main luabind header, in your case line 4.

That makes your program work - I tested it out on Linux (gcc 4.4.3).
 
In the past I found that I needed to prototype the operator << function before including operator.hpp, but now that seems to be not needed now.

Nigel

On Thu, 2010-07-08 at 09:59 -0700, Ken Smith 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

ps. Apologies if you receive two copies of this mail.  I was not
subscribed to the list when I sent it the first time.

------------------------------------------------------------------------------
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 very much, Nigel.  I missed the first line of section 8.4 of
the manual every time I read it which mentions that header file.

   With gratitude,
   Ken

On Thu, Jul 8, 2010 at 3:44 PM, Nigel Atkinson <[hidden email]> wrote:

> Hi Ken,
>
> Add #include "luabind/operator.hpp"
>
> after you include the main luabind header, in your case line 4.
>
> That makes your program work - I tested it out on Linux (gcc 4.4.3).
>
> In the past I found that I needed to prototype the operator << function
> before including operator.hpp, but now that seems to be not needed now.
>
> Nigel
>
> On Thu, 2010-07-08 at 09:59 -0700, Ken Smith 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
>
> ps. Apologies if you receive two copies of this mail.  I was not
> subscribed to the list when I sent it the first time.
>
> ------------------------------------------------------------------------------
> 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