PDA

View Full Version : Static Member - Multiple Definition Linker Error C++


SNYP40A1
June 21st, 2008, 04:21 AM
I have a class, call this class 'A', which is part of a C++ project that I am working on for a homework assignment. Currently, my project compiles fine. However, if I add one static variable to class A, even if it is not referenced anywhere else other than as a variable declaration in the class header file, I get several linking errors about the declaration being multiply defined. In fact, I get errors stating that the variable was first defined in another class which inherits from a class included in the header file that class A includes. Is there some kind of cyclic dependency going on? I would post code, but I am not allowed to.

jim_24601
June 23rd, 2008, 08:48 AM
Why can't you post code? It's a bit hard to figure out what's going wrong without it?

You didn't both define and declare the static member in the header file for the class, did you? Like:

file foo.h:

class Foo
{
static int sInt;
}

static int Foo::sInt;

That would cause every source file that includes foo.h to try to allocate its own static member. Move the definition line to the cpp file instead.

The "variable first defined in other class" thing I'm not so sure about, but one error in a C or C++ program can often beget a huge family of spurious ones.