Jump to content
  • Advertisement
Sign in to follow this  
Dan Forever

Linker Woes

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

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?

Share this post


Link to post
Share on other sites
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;

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!