'*'令牌之前的预期初始化程序

Ste*_*sku 4 c++ design-patterns

我正在尝试在Design Patterns一书中实现代码.我收到以下错误:

expected initializer before ‘*’ token
Run Code Online (Sandbox Code Playgroud)

对于这一行:

static Singleton *Singleton::itsInstance = 0;
Run Code Online (Sandbox Code Playgroud)

这是完整的代码.我正在使用g ++ 4.2.1来尝试编译它.

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;
}
Run Code Online (Sandbox Code Playgroud)

有任何想法吗?

Alo*_*ave 10

class Singleton {

};
 ^^^
Run Code Online (Sandbox Code Playgroud)

这个!并且,

static Singleton *Singleton::itsInstance = 0;
Run Code Online (Sandbox Code Playgroud)

替换为:

Singleton *Singleton::itsInstance = 0;
Run Code Online (Sandbox Code Playgroud)

你需要的static只是声明而不是定义.