1

I am going over a few design patterns C++, and I've come across this error.

class Singleton {
public:
    static Singleton* instance;
    static Singleton* getInstance() {
        if (instance == nullptr) {
            instance = new Singleton;
        }
        return instance;
    }

    int x;

private:
    Singleton() {};
};

Singleton* Singleton::instance = nullptr;


int main()
{
    Singleton foo1* = Singleton::getInstance(); // cannot access private member Singleton

}

I am also quite new to the material. Can I get some help in identifying what is wrong / how I can fix this? Also, I am well aware sington designs being 'anti-patterns', this is mostly for practice.

onesiumus
  • 279
  • 6
  • 26
  • 4
    Error in declaration of `foo1`: `Singleton foo1*` => `Singleton *foo1`. After that, no more problem of compilation – Garf365 Aug 02 '16 at 08:58
  • 1
    `getInstance` is clearly `public`. You need to give a name to the variable by placing `*` after the type not the variable name. – Mohit Jain Aug 02 '16 at 08:59
  • 1
    Could be an interesting question to ask how come you did not get a compilation error for `Singleton foo1*`. I guess that it has something to do with the lexical analysis of the code. Perhaps the compiler has considered `*=` as the intended operation. – barak manos Aug 02 '16 at 09:02
  • 1
    @barakmanos effectively, with this view, this question become interesting. What is the compilor ? In which version ? – Garf365 Aug 02 '16 at 09:08
  • This answer may be useful: http://stackoverflow.com/a/1008289/3807729 – Galik Aug 02 '16 at 09:11
  • @barakmanos The declaration `Singleton foo1` can only be followed by an initiliazer (aka {..}, (...), = ...) so * or *= are already clearly a syntax error. – Christophe Aug 02 '16 at 09:19
  • @Garf365 indeed, but it's only about the order of errors: GCC immediately spots the syntax errors. The compiler used there first spots that he can't build the Singleton beofre continuiing the parsing. – Christophe Aug 02 '16 at 09:20
  • @Christophe thanks for precision. No more suspense now – Garf365 Aug 02 '16 at 09:22
  • @Christophe: Hence the question of why the compiler did not emit an error on this remains interesting. – barak manos Aug 02 '16 at 09:26
  • 2
    @barakmanos GCC spots immediately the syntax error. VC++ spots several errors: C2143 for the missing ";" before "*", then he continues as if there was a statement end and finds C2248, that he can't access the private constructor (because for the moment he analyzes the `Singleton foo1` and notices he can't construct it). I guess OP did focus on the strangest error message instead of showing all the error messages... – Christophe Aug 02 '16 at 10:08

1 Answers1

3

Hey @mannerofallthings,

The problem is locate in your main function where you desire was to assign the internal Singleton to a pointer to a Singleton. But you have a typo there which tells the compiler that you want something else.

Singleton foo* = Singleton::getInstance();

which should be

Singleton* foo = Singleton::getInstance();

The interesting part here is Singleton foo* which you confused with Singleton* foo. So in the first part, the one which was wrong, the compiler tries to create a Singleton instance on the stack (that's the first compile error, it would then complain about the following asteriks later) and due to the private constructor the compiler yells at you that it cannot access Singleton's constructor.

Codebrewer
  • 130
  • 9