class Song {
private:
// singletong
Song();
Song(Song const&); // don't implement
void operator = (Song const&); //don't implement
public:
// Singleton method
static Song &getInstance(){
static Song song;
return song;
}
};
Run Code Online (Sandbox Code Playgroud)
如果我不打电话给班级,那就没问题了.我一打电话给Song课程就像这样:
Song::getInstance();
// also tried: Song &song = Song::getInstance();
Run Code Online (Sandbox Code Playgroud)
Xcode不再需要构建项目了.我收到这个错误:

任何想法为什么会这样?
您没有实现自从在getInstance()函数中实例化对象以来必须存在的构造函数:
static Song song;
Run Code Online (Sandbox Code Playgroud)
要么实现它内联(不受欢迎):
private:
// singletong
Song() {
// Your implementation goes here
}
Run Code Online (Sandbox Code Playgroud)
或者在编译单元(例如Sound.cpp)中实现它(首选):
Song::Song(){
// Your implementation goes here
}
Run Code Online (Sandbox Code Playgroud)