• Advertisement

Archived

This topic is now archived and is closed to further replies.

OOP beginner's question, using a function from a class

This topic is 5833 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Ok, this is a piece o' code I have in my main.h file, which is included in main.cpp
    
class Engine
{
public:

	int		InitScene(GLvoid);
	GLvoid		ResizeScene(GLsizei width, GLsizei height);
	int		DrawScene(GLvoid);
};
  
Now, if I try to use, let's say, InitScene in my main.cpp file:
  
int Engine::InitScene(GLvoid)
{
	// Lotsa stuff which doesn't matter


	return TRUE;
}
  
I get the error: error C2352: 'Engine::InitScene' : illegal call of non-static member function : see declaration of 'InitScene' Can anybody please tell me what is wrong with the above code and how to fix it? Thanx in advance... Edited by - Subotron on February 4, 2002 11:58:31 AM

Share this post


Link to post
Share on other sites
Advertisement
In your class declaration, instead of:
  
int InitScene( GLvoid );

try
  
static int InitScene( GLvoid );

Share this post


Link to post
Share on other sites
Well, that will work, but it may not be what you wanted to do.

static member functions can be called without a specific instance of the class. If you want to call a non-static member function, you need an instance of the class to call it from.

  
// static member

int main(void)
{
Engine::InitScene();
}

// non-static member

int main(void)
{
Engine e; // create an instance of an engine

e.InitScene(); // call the InitScene function

}



Share this post


Link to post
Share on other sites
why use a class if you are going to call the functions outside an instance?
these cases are useful for initialization of instances for example, in MFC:

CTime t = CTime::GetCurrentTime();

besides that, static functions inside a class are good for callbacks, or thread functions, but having only static functions inside a class makes no sence, of course I dont know how are you doing this, you might already know that.

Share this post


Link to post
Share on other sites
Yes, I know that

I just prefer to have all the functions related to the class in the class, they''re not all as useless as this one

Share this post


Link to post
Share on other sites

  • Advertisement