Re: tostring

Nicolas Goles <[email protected]>
Newsgroups gmane.comp.lang.lua.bind.user
Message-ID <[email protected]>
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 <[email protected]> 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
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.