2
class PossibilisticShellClustering
{
public:
    PossibilisticShellClustering(void);
    ~PossibilisticShellClustering(void);
    static void SetParameters(double deltaDistance);
    static  double deltaDistance
};

and i wanto to initialize static variable deltaDistance in function SetParameters. So in *.cpp file I wrote

void PossibilisticShellClustering::SetParameters(double deltaDistance)
{
    PossibilisticShellClustering::deltaDistance = deltaDistance;    
}

however I get linker erros

unresolved external symbol "public: static double PossibilisticShellClustering::deltaDistance" (?deltaDistance@PossibilisticShellClustering@@2NA)

Could somebody tell me why ?

PossibilisticShellClustering.obj
1
  • You have no semi-colon after static double deltaDistance? Commented Apr 30, 2011 at 22:40

1 Answer 1

2

You need to defined PossibilisticShellClustering::deltaDistance in a source file somewhere in your program, usually a .cc or .cpp file.

double PossibilisticShellClustering::deltaDistance;

What you have in the class body (or would have if it was terminated with a ;) is only a declaration. Static data members also need a definition.

Sign up to request clarification or add additional context in comments.

5 Comments

And explicitly initialise it, I would say.
@unapersson: It will be zero-initialized first and as he's writing a function for later explicit assignment, unless he has a special value that it needs to have during start up and explicit initializer seems a bit redundant.
I hate coming across statics that aren't initialised and then wondering if the guy that created them really wanted them to be default initialised to zero, or just forgot to initialise them to what they should be. Explicit initialisation only takes a few keystrokes, after all.
@unapersson: I hate coming across statics. ;-) Enforcing explicit initialization of statics even if you wanted the zero-initialized default is not something I can bring myself to care about greatly. Anyone who manages to forget to initialize a static to some non-zero value that actually matters needs to test their code more thoroughly IMHO.
Well, I have to disagree here. A good static analysis tool (or even a compiler) can detect non-explicitly initialised statics and IMHO should do so - after all, where's the loss? I agree that in the great scheme of things it probably is not Error #1, but if it is one more thing that can go wrong, why not diagnose it?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.