Horde3D
http://horde3d.org/forums/

error C2597: illegal reference to non-static member [SOLVED]
http://horde3d.org/forums/viewtopic.php?f=5&t=724
Page 1 of 1

Author:  Siavash [ 01.05.2009, 11:45 ]
Post subject:  error C2597: illegal reference to non-static member [SOLVED]

I'm trying to call C functions from Lua like this :
Code:
class app
{
   NodeHandle envRoot;

   // setting envRoot

   init()
   {
      // init lua
      lua_register(Lua, "getNodeHandle", getNodeHandle);
   }
   
   static int getNodeHandle (lua_State *L)
   {
      if (lua_tostring(L,-1) == "Model")
         if (Horde3D::findNodes(envRoot, lua_tostring(L, -2), SceneNodeTypes::Model) == 1)
            lua_pushnumber(L, Horde3D::getNodeFindResult(0));
      return 1;
   }
};

But I'm getting the following error :

error C2597: illegal reference to non-static member 'app::envRoot'

Author:  Volker [ 01.05.2009, 13:10 ]
Post subject:  Re: error C2597: illegal reference to non-static member

You can't access member variables of a class from a static context without an instance of that class.

Author:  Siavash [ 01.05.2009, 13:53 ]
Post subject:  Re: error C2597: illegal reference to non-static member

But how to solve this problem ?! I'm going to have a look at my old C++ book :)

Author:  Volker [ 01.05.2009, 13:55 ]
Post subject:  Re: error C2597: illegal reference to non-static member

One solution would be to make all class members static. But in this case you could also use a namespace instead, because a class having only static members does not make really sense, does it?

Author:  Siavash [ 01.05.2009, 14:07 ]
Post subject:  Re: error C2597: illegal reference to non-static member

Whats the advantage of using namespaces or making all of the functions static?

Author:  Volker [ 01.05.2009, 14:31 ]
Post subject:  Re: error C2597: illegal reference to non-static member

I'm not an expert regarding compilers, so I can't tell you what will be the difference on the generated code, but if you make all members static, I think the result will be nearly the same as if you are using namespaces.

It's more a matter of style/design. If you create a class, one would expect that there could be several instances of this class with all the things classes provide in the context of object orientated programming (like inheritance, etc.). If you are making all members static you don't have those capabilities so IMHO it does not make too much sense to put all those functions into a class.

If you want several instances of a class and use it within lua, you could also pass a pointer of the instance to lua and get it from the stack when calling a lua function. Then you can cast it in your C-lua function and use it. But that's all depending on what you are actually try to do with lua and C++.

Author:  Siavash [ 01.05.2009, 14:46 ]
Post subject:  Re: error C2597: illegal reference to non-static member

Volker wrote:
It's more a matter of style/design. If you create a class, one would expect that there could be several instances of this class with all the things classes provide in the context of object orientated programming (like inheritance, etc.). If you are making all members static you don't have those capabilities so IMHO it does not make too much sense to put all those functions into a class.
I'm using only one instance of app class at whole game. The second way looks amazing too.

EDIT : I've tried to define the envRoot as "static NodeHandle envRoot" but this causes some Linker problems. I want to call app member functions from Lua.

Author:  Siavash [ 01.05.2009, 16:16 ]
Post subject:  Re: error C2597: illegal reference to non-static member

Fixed that problem by changing "static int getNodeHandle()" to "int getNodeHandle()", but I'm getting another error :
Code:
error C3867: 'app::getNodeHandle': function call missing argument list; use '&app::getNodeHandle' to create a pointer to member

And by changing 'app::getNodeHandle' to '&app::getNodeHandle', I'm getting another error :
Code:
error C2664: 'lua_pushcclosure' : cannot convert parameter 2 from 'int (__thiscall app::* )(lua_State *)' to 'lua_CFunction'
        There is no context in which this conversion is possible

Author:  Volker [ 01.05.2009, 17:11 ]
Post subject:  Re: error C2597: illegal reference to non-static member

Because you can't cast class member functions to C functions. Since the methods of a class are bound to the instance of the class, the compiler can't know the address of the function at compile time.

Author:  Siavash [ 01.05.2009, 17:13 ]
Post subject:  Re: error C2597: illegal reference to non-static member

Isn't there anyway to do this? Perhaps you have experienced similar problems when coding the Horde3D Editor. IMHO this will make the Lua too useless when we can't call C functions from Lua easily.

Author:  Volker [ 01.05.2009, 17:17 ]
Post subject:  Re: error C2597: illegal reference to non-static member

You always need the instance of the class if you want to call pointers of class methods. To solve the lua problem you might want to take a look at the lua bindings within the editor. Doing a google search on lua and c++ might help you also.

Quote:
IMHO this will make the Lua too useless when we can't call C functions from Lua easily.

You can call C functions from Lua very easily, the problem is that you don't have C functions but C++ class methods.

Author:  Siavash [ 01.05.2009, 17:26 ]
Post subject:  Re: error C2597: illegal reference to non-static member

Volker wrote:
You always need the instance of the class if you want to call pointers of class methods. To solve the lua problem you might want to take a look at the lua bindings within the editor. Doing a google search on lua and c++ might help you also.
I've googled a lot about this problem, but couldn't find anything useful. I'll have a look at bindings. Thanks for help :wink:

Author:  Volker [ 01.05.2009, 17:34 ]
Post subject:  Re: error C2597: illegal reference to non-static member

Maybe this helps: http://loadcode.blogspot.com/2007/02/wrapping-c-classes-in-lua.html

Author:  Siavash [ 01.05.2009, 18:01 ]
Post subject:  Re: error C2597: illegal reference to non-static member

Indeed an interesting article :D

Page 1 of 1 All times are UTC + 1 hour
Powered by phpBB® Forum Software © phpBB Group
https://www.phpbb.com/