c ++ Meyers单例未定义引用 [英] c++ Meyers singleton undefined reference

查看:246
本文介绍了c ++ Meyers单例未定义引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码实现一个基本的Meyers单例:

I have the following code that implements a basic Meyers singletone:

#ifndef _cConfigFile_HH
#define _cConfigFile_HH

class cConfigFile {
public:
  static cConfigFile& getInstance() {
    static cConfigFile instance;
    return instance;
  };
private:
  cConfigFile();
};

#endif



我的编译器不允许我编译这个,给出以下错误:

My compiler doesn't allow me to compile this, giving the following error:

/include/cConfigFile.hh:7: undefined reference to `cConfigFile::cConfigFile()'



从错误中我理解我需要在.cpp文件中声明instance要声明cConfigFile :: instance,因为编译器说:

From the error I understand that I need to declare "instance" in a .cpp file, but am unable to declare cConfigFile::instance because the compiler says:


'cConfigFile cConfigFile :: instance'不是静态

‘cConfigFile cConfigFile::instance’ is not a static

我做错了什么?我在这里失踪了。

What am I doing wrong?? I'm lost here..

推荐答案

您忘了实现你的构造函数。

You forgot to implement your constructor.

这篇关于c ++ Meyers单例未定义引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆