Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

expected initializer before ‘*’ token

I am trying to implement the code in the Design Patterns book. I am getting the following error:

expected initializer before ‘*’ token

for this line:

static Singleton *Singleton::itsInstance = 0;

Here's the complete code. I am using g++ 4.2.1 to try and compile this.

class Singleton {
public:
    static Singleton *instance();
protected:
    Singleton();
private:
    static Singleton *itsInstance;
}

static Singleton *Singleton::itsInstance = 0;

Singleton *Singleton::instance()
{
    if (!itsInstance)
    {
        itsInstance = new Singleton;
    }
    return itsInstance;
}

Any ideas?

like image 443
Stephen Rasku Avatar asked Feb 06 '13 04:02

Stephen Rasku


1 Answers

class Singleton {

};
 ^^^

This! and also,

static Singleton *Singleton::itsInstance = 0;

replaced with:

Singleton *Singleton::itsInstance = 0;

You need the static only on the declaration not on the definition.

like image 199
Alok Save Avatar answered Sep 23 '22 22:09

Alok Save