2

I asked a question earlier today about singletons, and I'm having some difficulties understanding some errors I encountered. I have the following code:

Timing.h

class Timing {

public:
    static Timing *GetInstance();
private:
    Timing();
    static Timing *_singleInstance;
};

Timing.cpp

 #include "Timing.h"

 static Timing *Timing::GetInstance() {  //the first error
    if (!_singleInstance) {
        _singleInstance = new Timing();  //the second error
    }
    return _singleInstance;
}

There are two errors in this code which I can't figure out.

  1. The method GetInstance() is declared in the header as static. Why in the cpp file do I have to omit the word static? It gives the error: "cannot declare member function ‘static Timing* Timing::GetInstance()’ to have static linkage". The correct way to write it is:

    Timing *Timing::GetInstance() { ... }  
    
  2. Why can't I write _singleInstance = new Timing();? It gives the error: "undefined reference to Timing::_singleInstance". I solved this error by defining _singleInstance as a global var in the cpp file.

John Kugelman
  • 330,190
  • 66
  • 504
  • 555
Asher Saban
  • 4,523
  • 12
  • 44
  • 57
  • This is a bit off-topic but i feel the need to mention this: You asked a lot of questions at Stack Overflow. Most of them are answered correctly. Is there a reason causing you not to accept any of these answers? It might be frustrating for others to answer your questions, if you do not reward them. – jwueller Sep 08 '10 at 21:50

5 Answers5

5

1: static means "local linkage" when used for a function declaration/definition outside a class-declaration.

Local linkage means that the particular function can only be referenced from code inside this particular file, and that doesn't make much sense with a method in a class.

2: Since your class declaration can be included multiple times, the actual storage for the static member should be defined in the cpp-file:

#include "Timing.h"

Timing* Timing::_singleInstance;

Timing *Timing::GetInstance() {  //the first error
    if (!_singleInstance) {
        _singleInstance = new Timing();  //the second error
    }
    return _singleInstance;
}
Kleist
  • 7,455
  • 1
  • 24
  • 30
2

Referencing to question 2: You need to specify the static variable at the top of your cpp-file:

Timing* Timing::_singleInstance = NULL;
jwueller
  • 29,756
  • 4
  • 63
  • 69
  • yes i figured it out eventually, but i'm wondering if i already defined it in the header file, why can't i use it in the cpp file? – Asher Saban Sep 08 '10 at 21:40
  • you declared it in the header file but didn't define it. You essentially say that this class has such a (static) member but the compiler doesn't know in which object file you want to reserve memory for it. – Andre Holzner Sep 08 '10 at 21:42
  • Like Andre pointed out, you need to allocate memory for static variables before the actual program begins to operate. Static variables are _allways_ available. – jwueller Sep 08 '10 at 21:42
  • i understand now.. thank you all for your answers – Asher Saban Sep 08 '10 at 21:43
1
  1. static within a class means something completely different than static outside of it. Yeah, not the greatest design decision of C++, but, we have to live with it.

  2. I imagine the whining comes from the linker, and it's because you have declared that variable but never defined it, making it an undefined references. Just add in your .cpp file a line like:

    Timing* Timing::_singleInstance;
    
Alex Martelli
  • 811,175
  • 162
  • 1,198
  • 1,373
0
  1. yes, you have to omit the static in the .cpp file

  2. You'll have to 'reserve memory' for _singleInstance somewhere, e.g. by writing the following in the .cpp file:

    Timing *Timing::_singleInstance = NULL;

(outside the definition of the member functions)

Andre Holzner
  • 17,661
  • 6
  • 53
  • 61
-1

In the definition, you need to omit the static keyword. Its because that's teh syntax of C++. Nothing big.

Once you fix error number 1, error number 2 will be fixed automatically.

jrharshath
  • 24,957
  • 33
  • 95
  • 127