c++ - -Wunused-变量警告

标签 c++ warnings

我正在学校作业中实现单例设计模式,这是我的类头文件:

class Scheduler {
public:
    static Scheduler * instance();
    ~Scheduler();

private:
    Scheduler();
};

static Scheduler * _singleton = 0; // WARNING HERE

我的问题是我不断收到此错误:

../Scheduler.h:60:20: warning: ‘_singleton’ defined but not used [-Wunused-variable]

而且我们必须提交没有编译警告的作业。我如何摆脱这个警告?我不需要在头文件本身中使用 _singleton,所以我不确定该怎么做。我知道这很愚蠢,但仍然...

想法?

最佳答案

你的静态单例实例指针应该是一个类成员。目前它只是一个免费的指针。

class Scheduler {
// as before
private:
    Scheduler();
    static Scheduler* _singleton; // declare it in the class
};

在实现文件中:

Scheduler * Scheduler::_singleton = 0;

关于c++ - -Wunused-变量警告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10340515/

相关文章:

iphone - 在 iPad 上收到内存警告

android - SDL 一直无法在 Android 上找到字体

c++ - 将 unique_ptr 插入 map ,指针被销毁

c++ - 拆分逗号分隔的字符串

controller - Odoo 从 Web Controller 生成警告(弹出)

c++ - 带有 boost : linker(Id) Warning about visibility settings 的 xcode

c++ - Visual C++ 6初学者教程

c++ - Constexpr 值作为指针类型的非类型模板参数

php - "Illegal string offset"对象属性数组警告?

java - 奇怪的 "Resource leak: stream is never closed"如果在循环中抛出异常,则使用 try-with-resources