Linker Woes

Started by
4 comments, last by Lacutis 19 years, 1 month ago
Firstly, some sauce: (mmmmm sauce)

class Render
	{
	friend static class RenderManager;

	protected:
		static RenderManager*	m_pTheBoss;
		static MyDrawEngine*	m_pTheDrawEngine;
	public:
		Render(void);
	}

Render::Render(void)
	{
	Render::m_pTheBoss->Register(this);
	}

class RenderManager
	{
	public:
		MyDrawEngine* m_pTheDrawEngine;
	public:
		RenderManager();
	}

//Constructor for RenderManager
RenderManager::RenderManager()
	{
	//Create a new drawing engine
	m_pTheDrawEngine = new MyDrawEngine();

	//Make sure the renderables know who the daddy is
	Render::m_pTheBoss = this;
	Render::m_pTheDrawEngine = this->m_pTheDrawEngine;
	}


I can not for the life of me see why I get the following linker errors:

Render.obj : error LNK2001: unresolved external symbol "protected: static class RenderManager * Render::m_pTheBoss"
(?m_pTheBoss@Render@@1PAVRenderManager@@A)
Render.obj : error LNK2001: unresolved external symbol "protected: static class MyDrawEngine * Render::m_pTheDrawEngine"
(?m_pTheDrawEngine@Render@@1PAVMyDrawEngine@@A)
Debug/spac0r.exe : fatal error LNK1120: 2 unresolved externals
commenting out the two lines that reference the static variables of Render from RenderManager() leaves me with the top error. commenting out the line in Render() constructor that references the static variable removes the top error (though the error remains if the reference to the variable is still there in RenderManager()) ARGH! I can't see what I'm doing wrong. Even if it was a problem with friend, I shouldn't get an error from the Render() constructor, should I?
Advertisement
Those two pointers are delcared static, which means you need to define them somewhere in your code.

After your class put this:

RenderManager* Render::m_pTheBoss = NULL;
MyDrawEngine* Render::m_pTheDrawEngine = NULL;

I'm trying to initalise them in RenderManager(), but it's throwing a wobbly
You can't initialize them inside the class code.
Declaring a variable static means it isn't bound to an instance of a class and is able to be referenced without any instances of a class being initialized.

Thats why you have to initialize them outside of the class statement.

Additionally, you can't initialize them in another class like that. Especially since the "this" pointer is only valid within the context of an instance.

In other words, "this" only means something inside of a class method of an instance of a class.
Well, it's a linker error, not a compilation error.

'this' isn't being used outside of class instances.

The idea behind this code is that when an instance of Render is created, it informs the RenderManager. But it needs a pointer to RenderManager in order to do so, hence the static pointer.
I just reread your code trying to figure out what you are trying to do.
I copied this code and tried to get it to compile and its pretty much a mess.

What exactly are you trying to accomplish?
Do you imagine your game rendering to different devices at the same time?

This topic is closed to new replies.

Advertisement