Problem with derived classes and overloaded member functions

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

Problem with derived classes and overloaded member functions

Saner  Daniel
Hello,

I have previously posted this with an unregistered e-Mail, but the message was never approved, so excuse me if it will appears again one day...
I'm very new to both Lua and luabind. I have a problem and I'm not sure whether I'm doing something wrong in handling an overloaded member function, or the derived class in which they are defined. I have used luabind with overloaded functions before, and I've used it with derived classes before, both without problem, but now that I tried combining the two, I am oscillating between compilation errors and runtime errors.

My base class is called 'IStreamPlugin' and has an overloaded member function called 'help', one of them taking no arguments and another one taking a 'const char *'. A derived class called 'PluginCLGeneric' implements these two functions. The way I tried to register them using 'luabind' is:

luabind::module(L) [
   luabind::class_<PluginCLGeneric>("PluginCLGeneric")
       .def(luabind::constructor<StreamManager&, LogManager&, lua_State*>())
       .def("help", (void(PluginCLGeneric::*)())&PluginCLGeneric::help)
       .def("help", (void(PluginCLGeneric::*)(const char*))&PluginCLGeneric::help)
   ];

This compiles fine, but when trying to call either of the 'help' functions from Lua, I get this error:

No matching overload found, candidates:
void help(PluginCLGeneric&,char const*)
void help(PluginCLGeneric&)

I figured that the problem might stem from the fact that I export only the derived class to Lua, and not the respective base class (although the exported functions do not depend on anything that stems from the base class). I rewrote my luabind section like this:

luabind::module(L) [
   luabind::class_<IStreamPlugin>("IStreamPlugin"),
   luabind::class_<PluginCLGeneric, IStreamPlugin>("PluginCLGeneric")
       .def(luabind::constructor<StreamManager&, LogManager&, lua_State*>())
       .def("help", (void(PluginCLGeneric::*)())&PluginCLGeneric::help)
       .def("help", (void(PluginCLGeneric::*)(const char*))&PluginCLGeneric::help)
   ];

according to the luabind documentation. However, this yields the following compilation error:

luabind/detail/inheritance.hpp(136) : error C2243: 'static_cast' : conversion from 'PluginCLGeneric *' to 'bases_t *' exists, but is inaccessible

I tried also exporting the constructor and the base class' versions of the functions (although they are pure virtual) with the same results. Also, if I change the second 'luabind::class' line to incorporate 'bases', i.e.

luabind::class_<PluginCLGeneric, luabind::bases<IStreamPlugin>>("PluginCLGeneric")

the error message changes to:

luabind/detail/inheritance.hpp(136) : error C2243: 'static_cast' : conversion from 'PluginCLGeneric *' to 'IStreamPlugin *' exists, but is inaccessible

I am a bit at a loss what it means that this cast is supposed to be inaccessible, or even needed. Can someone point me in the right direction?

I am using Visual C++ 2008 and a statically linked luabind-0.9.

Cheers
Daniel
------------------------------------------------------------------------------
Download Intel&#174; Parallel Studio Eval
Try the new software tools for yourself. Speed compiling, find bugs
proactively, and fine-tune applications for parallel performance.
See why Intel Parallel Studio got high marks during beta.
http://p.sf.net/sfu/intel-sw-dev
_______________________________________________
luabind-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user
Reply | Threaded
Open this post in threaded view
|

Re: Problem with derived classes and overloaded member functions

Nigel Atkinson-2
How are you calling the 'help' function in Lua?

Remember that:

classInstance:method( param )

is just syntactic sugar for

classInstance.method( classInstance, param )

so in your case if you called help as

instance.help()

it will not match any of your bound functions, as the 'self' argument is
not given.  I personally see this all the time, as I have a Lua based
console in the game I'm writing, and I often misstype '.' where I should
have written ":"

Nigel

On Mon, 2010-03-22 at 20:39 +0100, Saner Daniel wrote:

> Hello,
>
> I have previously posted this with an unregistered e-Mail, but the message was never approved, so excuse me if it will appears again one day...
> I'm very new to both Lua and luabind. I have a problem and I'm not sure whether I'm doing something wrong in handling an overloaded member function, or the derived class in which they are defined. I have used luabind with overloaded functions before, and I've used it with derived classes before, both without problem, but now that I tried combining the two, I am oscillating between compilation errors and runtime errors.
>
> My base class is called 'IStreamPlugin' and has an overloaded member function called 'help', one of them taking no arguments and another one taking a 'const char *'. A derived class called 'PluginCLGeneric' implements these two functions. The way I tried to register them using 'luabind' is:
>
> luabind::module(L) [
>    luabind::class_<PluginCLGeneric>("PluginCLGeneric")
>        .def(luabind::constructor<StreamManager&, LogManager&, lua_State*>())
>        .def("help", (void(PluginCLGeneric::*)())&PluginCLGeneric::help)
>        .def("help", (void(PluginCLGeneric::*)(const char*))&PluginCLGeneric::help)
>    ];
>
> This compiles fine, but when trying to call either of the 'help' functions from Lua, I get this error:
>
> No matching overload found, candidates:
> void help(PluginCLGeneric&,char const*)
> void help(PluginCLGeneric&)
>
> I figured that the problem might stem from the fact that I export only the derived class to Lua, and not the respective base class (although the exported functions do not depend on anything that stems from the base class). I rewrote my luabind section like this:
>
> luabind::module(L) [
>    luabind::class_<IStreamPlugin>("IStreamPlugin"),
>    luabind::class_<PluginCLGeneric, IStreamPlugin>("PluginCLGeneric")
>        .def(luabind::constructor<StreamManager&, LogManager&, lua_State*>())
>        .def("help", (void(PluginCLGeneric::*)())&PluginCLGeneric::help)
>        .def("help", (void(PluginCLGeneric::*)(const char*))&PluginCLGeneric::help)
>    ];
>
> according to the luabind documentation. However, this yields the following compilation error:
>
> luabind/detail/inheritance.hpp(136) : error C2243: 'static_cast' : conversion from 'PluginCLGeneric *' to 'bases_t *' exists, but is inaccessible
>
> I tried also exporting the constructor and the base class' versions of the functions (although they are pure virtual) with the same results. Also, if I change the second 'luabind::class' line to incorporate 'bases', i.e.
>
> luabind::class_<PluginCLGeneric, luabind::bases<IStreamPlugin>>("PluginCLGeneric")
>
> the error message changes to:
>
> luabind/detail/inheritance.hpp(136) : error C2243: 'static_cast' : conversion from 'PluginCLGeneric *' to 'IStreamPlugin *' exists, but is inaccessible
>
> I am a bit at a loss what it means that this cast is supposed to be inaccessible, or even needed. Can someone point me in the right direction?
>
> I am using Visual C++ 2008 and a statically linked luabind-0.9.
>
> Cheers
> Daniel
> ------------------------------------------------------------------------------
> Download Intel&#174; Parallel Studio Eval
> Try the new software tools for yourself. Speed compiling, find bugs
> proactively, and fine-tune applications for parallel performance.
> See why Intel Parallel Studio got high marks during beta.
> http://p.sf.net/sfu/intel-sw-dev
> _______________________________________________
> luabind-user mailing list
> [hidden email]
> https://lists.sourceforge.net/lists/listinfo/luabind-user



------------------------------------------------------------------------------
Download Intel&#174; Parallel Studio Eval
Try the new software tools for yourself. Speed compiling, find bugs
proactively, and fine-tune applications for parallel performance.
See why Intel Parallel Studio got high marks during beta.
http://p.sf.net/sfu/intel-sw-dev
_______________________________________________
luabind-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user
Reply | Threaded
Open this post in threaded view
|

Re: Problem with derived classes and overloaded member functions

Saner  Daniel
In reply to this post by Saner Daniel
Thank you for the quick response.
(I hope I didn't mess up the 'reply-to')
Unfortunately this was not the problem, I am always calling the function with the double dot. In fact, the error message is exactly the same though as if I was writing it with the dot and no 'self' argument.

I'm afraid to say that only now I noticed that the problem does in fact not relate to the functions being overloaded. I failed to notice that by chance, *all* functions of the class in questions were overloaded. I added a non-overloaded dummy function to the class and it gives the same error:

> obj:doNothing()
No matching overload found, candidates:
void doNothing(PluginCLGeneric&)

I was thinking that maybe this is a problem related to how I register the object itself with Lua. The object is not created directly by calling the constructor from Lua, but a factory function which takes the name to give the object as a string argument. Calling this factory from Lua creates the object and then registers it as a global:

luabind::globals(L)[objectName] = createdInstance;

where 'createdInstance' is a pointer to the created object. Also, the class of the object and its functions have been registered with Luabind beforehand. This multi-step process was necessary due to the plugin architecture I am using it for. The process cannot be a complete failure though, as the object is accessible from Lua under 'objectName', and also the functions of the class have been registered. If any of these are not given, the error message does not reference the possible overload candidates anymore.
As the exact same procedure for an object of a non-derived class that is created at startup works flawlessly, I am lead to believe that it is indeed a problem of inheritance, but I was still not able to find the culprit. Maybe Lua is confused about the class of the created object?

Daniel

> How are you calling the 'help' function in Lua?
>
> Remember that:
>
> classInstance:method( param )
>
> is just syntactic sugar for
>
> classInstance.method( classInstance, param )
>
> so in your case if you called help as
>
> instance.help()
>
> it will not match any of your bound functions, as the 'self' argument is
> not given.  I personally see this all the time, as I have a Lua based
> console in the game I'm writing, and I often misstype '.' where I should
> have written ":"
>
> Nigel
------------------------------------------------------------------------------
Download Intel&#174; Parallel Studio Eval
Try the new software tools for yourself. Speed compiling, find bugs
proactively, and fine-tune applications for parallel performance.
See why Intel Parallel Studio got high marks during beta.
http://p.sf.net/sfu/intel-sw-dev
_______________________________________________
luabind-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user
Reply | Threaded
Open this post in threaded view
|

Re: Problem with derived classes and overloaded member functions

Daniel Wallin
On 3/24/10 5:02 AM, Saner Daniel wrote:

> Thank you for the quick response.
> (I hope I didn't mess up the 'reply-to')
> Unfortunately this was not the problem, I am always calling the
> function with the double dot. In fact, the error message is exactly the
> same though as if I was writing it with the dot and no 'self' argument.
>
> I'm afraid to say that only now I noticed that the problem does in
> fact not relate to the functions being overloaded. I failed to notice
> that by chance, *all* functions of the class in questions were
> overloaded. I added a non-overloaded dummy function to the class and it
> gives the same error:

>> obj:doNothing()
> No matching overload found, candidates:
> void doNothing(PluginCLGeneric&)
>
> I was thinking that maybe this is a problem related to how I
> register
> the object itself with Lua. The object is not created directly by
> calling the constructor from Lua, but a factory function which takes the
> name to give the object as a string argument. Calling this factory from
> Lua creates the object and then registers it as a global:
>
> luabind::globals(L)[objectName] = createdInstance;
>
> where 'createdInstance' is a pointer to the created object. Also,
> the
> class of the object and its functions have been registered with Luabind
> beforehand. This multi-step process was necessary due to the plugin
> architecture I am using it for. The process cannot be a complete failure
> though, as the object is accessible from Lua under 'objectName', and
> also the functions of the class have been registered. If any of these
> are not given, the error message does not reference the possible
> overload candidates anymore.
> As the exact same procedure for an object of a non-derived class
> that
> is created at startup works flawlessly, I am lead to believe that it is
> indeed a problem of inheritance, but I was still not able to find the
> culprit. Maybe Lua is confused about the class of the created object?


It shouldn't be. Everything in your post looks reasonable, so I guess
this could be a bug. Could you possibly reduce this to a failing test
that I can run?

--
Daniel Wallin
BoostPro Computing
http://www.boostpro.com

------------------------------------------------------------------------------
Download Intel&#174; Parallel Studio Eval
Try the new software tools for yourself. Speed compiling, find bugs
proactively, and fine-tune applications for parallel performance.
See why Intel Parallel Studio got high marks during beta.
http://p.sf.net/sfu/intel-sw-dev
_______________________________________________
luabind-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user
Reply | Threaded
Open this post in threaded view
|

Re: Problem with derived classes and overloaded member functions

Saner  Daniel

It shouldn't be. Everything in your post looks reasonable, so I guess
this could be a bug. Could you possibly reduce this to a failing test
that I can run?

It seems to be only happening with functions that are defined in the inheriting class, but were not declared in the base class. As long as it is declared in the base class and redefined in the inheriting class, the correct, redefined function is called. I can boil it all down to this:

I have a base class 'IPlugin' with a function
void base_f() {
  cout << "Base" << endl;
}

which I register with luabind like this:
luabind::module(L) [
        luabind::class_<IPlugin>("IPlugin")
        .def("base_f", &IPlugin::base_f)
];

I also have a derived class 'GenericPlugin':
class GenericPlugin : public IPlugin

which redefines base_f:
void base_f() {
  cout << "Derived" << endl;
}

and has an additional function der_f:
void der_f() {
  cout << "New" << endl;
}

I register the derived class and the new function like this:
luabind::module(L) [
        luabind::class_<GenericPlugin, IPlugin>("GenericPlugin")
        .def(luabind::constructor<>())
        .def("der_f", &GenericPlugin::der_f)
];

I can instantiate GenericPlugin from Lua (even using the constructor function and subsequently registering the created object as a global using luabind::globals(L)) and call 'base_f()' on it, which writes "Derived" to cout. However, when I try to call 'der_f()' on it, I again get:
No matching overload found, candidates:
void der_f(GenericPlugin&)

I suppose I have completely misunderstood something about the way derived classes are handled in luabind?

Thanks
Daniel

------------------------------------------------------------------------------
Download Intel&#174; Parallel Studio Eval
Try the new software tools for yourself. Speed compiling, find bugs
proactively, and fine-tune applications for parallel performance.
See why Intel Parallel Studio got high marks during beta.
http://p.sf.net/sfu/intel-sw-dev
_______________________________________________
luabind-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user