0

I am implementing the singleton design pattern in a school assignment, and this is my class header file:

class Scheduler {
public:
    static Scheduler * instance();
    ~Scheduler();

private:
    Scheduler();
};

static Scheduler * _singleton = 0; // WARNING HERE

My problem is that I keep getting this error:

../Scheduler.h:60:20: warning: ‘_singleton’ defined but not used [-Wunused-variable]

And we have to submit assignments with no compilation warnings. How do I get rid of this warning? There's no need for me to use _singleton in the header file itself so I'm not sure what to do. I know it's stupid, but still...

Ideas?

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
yotamoo
  • 5,334
  • 13
  • 49
  • 61
  • 1
    You aren't trying to compile the header file, are you? – Kendall Frey Apr 26 '12 at 19:59
  • remove the line that reports the error? – KevinDTimm Apr 26 '12 at 20:00
  • Who wrote the header, you or the teacher? If you did it, remove the declaration of the `_singleton` from the header; it would limit you to one Scheduler per source file, not one Scheduler, period. If the teacher did it and you can't modify it, then you're stuck with asking him how to get rid of the warning, I think. – Jonathan Leffler Apr 26 '12 at 20:03
  • Was the answer useful at all, or are you still having problems? – juanchopanza Apr 28 '12 at 07:23

1 Answers1

4

Your static singleton instance pointer should be a class member. Currently it is just a free pointer.

class Scheduler {
// as before
private:
    Scheduler();
    static Scheduler* _singleton; // declare it in the class
};

and in the implementation file:

Scheduler * Scheduler::_singleton = 0;
juanchopanza
  • 223,364
  • 34
  • 402
  • 480