static function

I have a static function on my manager class that i am trying to use to set a private member in the manager class.

I will be trying to access to access this static function inside the definition of a function in another class.


Here an example of the class
1
2
3
4
5
6
7
8
9
10
11
class Manager
{
public:
	Manager(void);
	~Manager(void);

	static void			SetFloatingTexture(SDL_Texture* texture);
private:

	SDL_Texture*			floatingTexture;
};


here is the function definition
1
2
3
4
void Manager::SetFloatingTexture(SDL_Texture* texture)
{
	floatingTexture = texture;
}



there is some sort of linker error. does anyone see what my error is here?

any help would be great




Last edited on
There is no linker error, there is a comppilation error. You are trying to access an instance member variable (floatingTexture) from a static member function (SetFloatingTexture). Why do you even want it to be static?
Non-static members exist for each object.
Static members exist once for all objects.


Therefore...

Each 'Manager' object has its own copy of your 'floatingTexture' because it is non-static.

But your 'SetFloatingTexture' function has no object associated with it because it is static.

So in your SetFloatingTexture function... you are trying to access floatingTexture... but which floatingTexture? There could be any number of them!

Example:
1
2
3
4
5
6
7
Manager A;  // 'A' has its own floatingTexture
Manager B;  // so does 'B'

// A.floatingTexture and B.floatingTexture are two different textures

Manager::SetFloatingTexture( foo ); // <- ? which floating texture are we setting?
    // A's?  B's?  some other object's? 
You cannot access a non static member inside a static method unless you explicitly make available the object instance inside the member function.(Pass object instance explicitly as argument or use a global instance which can be accessed inside the function)

For a non static member function an implicit this pointer is passed as the first argument to the function. The this pointer is dereferenced inside the member function to access the members. static members are not passed with the implicit this pointer so you cannot access non static members inside the function unless you explicitly get the object inside the member function.


Copied From:
http://stackoverflow.com/questions/15847307/cant-access-private-class-members-inside-of-static-method
i apologize i didnt update correctly.

i had changed the private member into a static variable

static SDL_Texture* floatingTexture;

and these are my errors:
1
2
Error	2	error LNK1120: 1 unresolved externals	C:a path....  
\mapeditor\Debug\sdl2.0.exe	sdl2.0



1
2
Error	1	error LNK2001: unresolved external symbol "public: static struct SDL_Texture * Manager::floatingTexture" (?floatingTexture@Manager@@2PAUSDL_Texture@@A)	
a path....     \mapeditor\sdl2.0\Manager.obj	sdl2.0



Last edited on
You have to instantiate static members.

Put this in your cpp file:

 
SDL_Texture* Manager::floatingTexture;
Topic archived. No new replies allowed.