Sign in to follow this  

One or more multiply defined symbols found??

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

Hey, I've been having a hell of a time debugging this project (C++ noob) and I've reached an error that I can not figure out. The project compiles fine but when it tries to link, it gives the following: SimpleWindowClass.obj : error LNK2005: "private: static class GameApplication * GameApplication::pApp" (?pApp@GameApplication@@0PAV1@A) already defined in GameApplication.obj Debug/GameApplication.exe : fatal error LNK1169: one or more multiply defined symbols found note**(SimpleWindowClass is the class that inherits GameApplication) I'm trying to create a class where the user can only create one instance of the object. pApp is the pointer to the class. The constructor looks like this: assert(pApp==0); pApp=this; Anyway, here's the header to the proj. I'm pretty sure the error must originate in there since its the only place that pApp is defined...

#include "windows.h"

#define WINDOW_CLASS_NAME "WINCLASS1"


class GameApplication  
{
public:
	GameApplication();
	~GameApplication();

protected:
	virtual bool AppBegin()		{ return true;}
	virtual bool AppEnd()		{ return true;}
	virtual void GetWindowDims(long& w, long& h)	{ w=640; h=480;}
	virtual LPCSTR GetTitle()	{ return "title";}
	virtual bool ProcessMessage(UINT Message, WPARAM wParam, LPARAM lParam)		{return false;}
	virtual bool KeyDown (long VirtKey, long KeyData)	{return false;}
	virtual bool KeyUp (long VirtKey, long KeyData)		{return false;}
	virtual bool Paint(WPARAM, LPARAM)	{return false;}

private:
	static GameApplication* pApp;
	HINSTANCE appInstance;
	HWND appWindow;

	friend int APIENTRY WinMain( HINSTANCE, HINSTANCE, TCHAR*, int);
	static LRESULT CALLBACK WndProc( HWND, UINT, WPARAM, LPARAM);
	int MemberWinMain (HINSTANCE, int);
	bool InitWindow(int ShowFlag);
};

GameApplication* GameApplication::pApp=0;




Any help would be greatly appreciated.

Share this post


Link to post
Share on other sites
No, include guards will not solve his problem. As a rule of thumb, include guards will never solve any linker error, only compiler errors.

Quote:

I'm pretty sure the error must originate in there since its the only place that pApp is defined...


Nope, this is far from being the only place where it is defined. Whenever your header file is included in another file, that header file's contents are actually written to the including file. This means every file in your project that includes this header file will contain the line:

GameApplication* GameApplication::pApp=0;

That's quite problematic for the linker, because this variable will have been defined (and compiled) more than once: one definition per file in which the line appears.

To solve this problem, you need to do two things:
- Remove the line from a file that is included in other files (usually a .h header file)
- Put the line in a file that is compiled (usually a .cpp file), and only one.

Share this post


Link to post
Share on other sites
Thx for your help guys.

I tried the include guards but as ToohrVyk predicted, it still gave me the same error. I took

GameApplication* GameApplication::pApp=0;

out of the header file and put it into the GameApplication.cpp file. I think the reason I was getting this is because I have another class SimpleWindowClass.cpp that inherits GameApplication.cpp.

It now compiles and I'm now following a new trail of bugs :)



sDUck

Share this post


Link to post
Share on other sites

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