Public Group

# One or more multiply defined symbols found??

This topic is 5103 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## 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 on other sites
what you need are inclusion headers.
they are this:
#ifndef NAME_OF_FILE_H#define NAME_OF_FILE_H// CODE GOES HERE#endif

and that will solve the current problem you have now.

##### 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 on other sites

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

1. 1
2. 2
3. 3
4. 4
frob
15
5. 5

• 11
• 20
• 12
• 13
• 14
• ### Forum Statistics

• Total Topics
632144
• Total Posts
3004427

×