#### Archived

This topic is now archived and is closed to further replies.

# Linker problem with static member variables

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

## Recommended Posts

Ok, I have a class that looks something like this (this is Window.h):
#ifndef _WINDOW_H
#define _WINDOW_H

class CWindow
{
public:
static void SetFullScreen(bool isFullScreen);
static bool IsFullScreen();
//... commented for space
static const LPSTR APP_NAME;

private:
static bool s_IsFullScreen;
//... ditto
};

bool CWindow::s_IsFullScreen = false;
const LPSTR CWindow::APP_NAME = "Doodling";

#endif

Now, I have two .cpps that include this file: Window.cpp, and Doodling.cpp (don't ask.) When it comes time to build, I keep getting the following errors: Window.obj : error LNK2005: "public: static char * CWindow::APP_NAME" (?APP_NAME@CWindow@@2QADA) already defined in Doodling.obj Window.obj : error LNK2005: "private: static bool CWindow::s_IsFullScreen" (?s_IsFullScreen@CWindow@@0_NA) already defined in Doodling.obj Debug/Doodling.exe : fatal error LNK1169: one or more multiply defined symbols found I only declared and defined them once - in Window.h - so I can't figure out what it's talking about. Any help would be appreciated. [EDIT] Remembered to put in the sentinel so you know I have it -Normie "But time flows like a river... and history repeats." ... So...what about beaver dams? Edited by - normie on August 5, 2001 2:30:08 PM

##### Share on other sites
I had a similar problem that went away when I initialized the class statics in the same file as the function definitions. I guess, in your case, that would be Window.cpp (or doodling.cpp.)

My guess is that they cannot be initialized until an instance has been created - which would be in the .cpp - since the statics are shared. This might help...

##### Share on other sites
Absolutely. Never define variables or functions (unless they''re inline functions) in header files. It won''t work.

##### Share on other sites
Really? Well, damn, learn something new every day.

-Normie

"But time flows like a river... and history repeats."
...