加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

Xcode中的C Singleton

发布时间:2020-12-14 18:08:31 所属栏目:百科 来源:网络整理
导读:我正在尝试使用 Xcode在C中创建一个Singleton类.这是一个非常基础的类,我得到一个我不理解的链接器错误.可以帮忙吗? 这是类头文件: #ifndef _NETWORK_H_#define _NETWORK_H_#include iostream#include list#include "Module.h"using namespace std;/* * As
我正在尝试使用 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)

解决方法

您需要在某处为Network :: _ instance声明实际存储.可能是impl.文件.

尝试添加到您的impl文件:

Network *Network::_instance=0;

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读