Re: tostring

Ken Smith <[email protected]>
Newsgroups gmane.comp.lang.lua.bind.user
Message-ID <[email protected]>
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 <[email protected]> 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
> [email protected]
> 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
> [email protected]
> 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
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.