小编Nic*_*s S的帖子

Xcode中的C++ Singleton

我正在尝试使用Xcode在C++中创建一个Singleton类.这是一个非常基础的类,我得到一个我不理解的链接器错误.可以帮忙吗?

这是类头文件:

#ifndef _NETWORK_H_
#define _NETWORK_H_

#include <iostream>
#include <list>
#include "Module.h"

using namespace std;

/*
 * Assume only one network can run at a time 
 * in the program. So make the class a singleton.
 */
class Network {
private:
    static Network* _instance;
    list<Module*> _network;

public:
    static Network* instance();
};

#endif
Run Code Online (Sandbox Code Playgroud)

这是impl文件:

#include "Network.h"

Network* Network::instance() {
    if (!_instance)
        _instance = new Network();
    return _instance;
}
Run Code Online (Sandbox Code Playgroud)

这是编译器错误:

Undefined symbols for architecture x86_64:
  "Network::_instance", referenced from:
      Network::instance() in Network.o
ld: …
Run Code Online (Sandbox Code Playgroud)

c++ xcode singleton

4
推荐指数
1
解决办法
914
查看次数

标签 统计

c++ ×1

singleton ×1

xcode ×1