Stephen Rasku
Stephen Rasku

Reputation: 2682

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?

Upvotes: 5

Views: 26521

Answers (2)

EClaesson
EClaesson

Reputation: 1618

You are missing a semicolon after your class definition, and you do not want the static.

static Singleton *Singleton::itsInstance = 0;

should be

Singleton *Singleton::itsInstance = 0;

Upvotes: 1

Alok Save
Alok Save

Reputation: 206518

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.

Upvotes: 17

Related Questions