Static Member Fucntions File Scope

Started by
2 comments, last by Frank_Li 17 years, 11 months ago
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?
Advertisement
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.
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.
John BoltonLocomotive Games (THQ)Current Project: Destroy All Humans (Wii). IN STORES NOW!
Thanks guys

This topic is closed to new replies.

Advertisement