Sign in to follow this  

Static Member Fucntions File Scope

This topic is 4225 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

Hello, first let me say that I'm a Java programer trying to complete a project in C++. I have a SpriteManager class and it has two static members, a static STL list and a static add function.
SpriteManager.h

class SpriteManager{
private:
	static std::list<Sprite*> spriteList;
	bool collision(Sprite* sp,Sprite* sp2);
	void checkCollisions();

public:
	SpriteManager();
	~SpriteManager();

        static void add(Sprite* sprite);
	       void updateSprites();
	       void drawSprites(HDC hDC);
	
};

Here is how I define the add() method...uh, function.
In SpriteManager.cpp

...//other functions

static void SpriteManager:: add(Sprite* sp){
     using namespace std;
     if(sp){
	  if(spriteList.size() == 0)
             spriteList.push_back(sp);
           else{
		 list<Sprite*>::iterator iter;
		 for(iter = spriteList.begin();iter != spriteList.end();iter++)
		    if( sp->getZOrder() > (*iter)->getZOrder()){
		        spriteList.insert(iter,sp);
		        return;
		     }
		     spriteList.push_back(sp);
	    }
      }
      else 
          MessageBox(NULL,TEXT("Add Null Sprite"),NULL,MB_OK);
}//end method

I get this error: error C2724: 'SpriteManager::add' : 'static' should not be used on member functions defined at file scope This error points to the add() function. I don't know how to fix this because I don't know what the error means. Would someone lend me a hand?

Share this post


Link to post
Share on other sites
Don't repeat the "static" modifier in the source file (as the error says, it shouldn't be used). Having it in the header is required, and only having it one place is sufficient.

Share this post


Link to post
Share on other sites
The static keyword is overloaded -- it has several meanings which depend on the scope and are mutually exclusive. static used in a class declaration is very different from static used in the file scope (outside of a function or class declaration), and both are very different from static used in a function. The error you got describes the problem very succinctly -- a member function/variable can't be declared static in the file scope.

Anyway, that's a long-winded way of telling you to just remove "static" from the function definition.

Share this post


Link to post
Share on other sites

This topic is 4225 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this