我正在尝试使用
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
这是impl文件:
#include "Network.h" Network* Network::instance() { if (!_instance) _instance = new Network(); return _instance; }
这是编译器错误:
Undefined symbols for architecture x86_64: "Network::_instance",referenced from: Network::instance() in Network.o ld: symbol(s) not found for architecture x86_64 clang: error: linker command Failed with exit code 1 (use -v to see invocation)