0

I'm trying to get the first basic singleton example from Design Patterns working, but this has me stumped.

This code compiles cleanly with g++ -c Singleton.cpp:

class Singleton {
public:
    static Singleton* Instance();
protected:
    Singleton();
private:
    static Singleton* _instance;
};

Singleton* Singleton::_instance = 0;

Singleton* Singleton::Instance() {
    if (_instance == 0) {
        _instance = new Singleton;
    }
    return _instance;
}

But when I add a skeletal main() and compile with g++ Singleton.cpp I get undefined reference to 'Singleton::Singleton()'.

What am I missing?

Community
  • 1
  • 1
Daniel
  • 445
  • 2
  • 12
  • You declared a constructor (you do not need), but left out he definition. –  Feb 08 '16 at 18:54
  • @DieterLücking _"you do not need"_ That's not entirely true. At least it should be made `private`. – πάντα ῥεῖ Feb 08 '16 at 18:57
  • @πάνταῥεῖ sure - damn anti pattern –  Feb 08 '16 at 19:00
  • @DieterLücking I knew it was something stupidly basic. Cheers. – Daniel Feb 08 '16 at 19:09
  • @πάνταῥεῖ Although it is probably a dupe, it's also the literal textbook example. I'm guessing a few folks might have the same problem. – Daniel Feb 08 '16 at 19:12
  • @Daniel Well, I could give your question an upvote so future researchers may benefit from finding it. On the other hand it's a very trivial mistake you made that's clearly covered in the marked duplicate and seems to be an outcome of low research efforts. I think I'll just leave everything as is. Be happy having your answer. – πάντα ῥεῖ Feb 08 '16 at 19:15

1 Answers1

5

You never added a definition for

Singleton();

Which is used in Singleton* Singleton::Instance() by

_instance = new Singleton;

Typically you should and can layout a singleton like:

class Singleton {
public:
    static Singleton* Instance() { static Singleton s; return &s; }
    Singleton(const Singleton&) = delete;
    void operator=(const Singleton&) = delete;
private:
    Singleton() = default;
};
NathanOliver
  • 171,901
  • 28
  • 288
  • 402
  • Great answer, I'll definitely be using that snippet. But it strays a bit far from the textbook example, maybe you could add a bit about using a default constructor? – Daniel Feb 08 '16 at 19:21
  • 1
    @Daniel `Singleton() = default;` is basically just a C++11 way to write `Singleton(){}`. If forces the compile to generate `Singleton(){}`; – NathanOliver Feb 08 '16 at 19:25
  • Thanks, Nathan! What does `= delete` mean then? Is it a syntax sugar of what? – Tim Sep 28 '17 at 20:47
  • @Tim It tells the compile that the function does not exits and using it will generate a nice compiler error saying you are trying to use a deleted function. In the days before C++11 you would have to declare it as private and not implement it and the error message about using it wasn't as nice – NathanOliver Sep 28 '17 at 20:50
  • Thanks. Is `_instance = new Singleton;` the same as `_instance = new Singleton();`? Which one is preferred? – Tim Sep 28 '17 at 20:54
  • @Tim In this case they are the same. Which is preferred is really a matter of style. – NathanOliver Sep 28 '17 at 21:06