- 变量警告 [英] -Wunused-variable warning

查看:176
本文介绍了 - 变量警告的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am implementing the singleton design pattern in a school assignment, and this is my class header file:

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

private:
    Scheduler();
};

static Scheduler * _singleton = 0; // WARNING HERE

我的问题是我一直收到这个错误:

My problem is that I keep getting this error:

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

我们必须提交没有编译警告的作业。如何摆脱这个警告?没有必要在头文件本身使用 _singleton ,所以我不知道该怎么办。我知道这很蠢,但还是...

And we have to submit assignments with no compilation warnings. How do I get rid of this warning? There's no need for me to use _singleton in the header file itself so I'm not sure what to do. I know it's stupid, but still...

想法?

推荐答案

p>您的静态单例实例指针应该是类成员。目前只是一个自由的指针。

Your static singleton instance pointer should be a class member. Currently it is just a free pointer.

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

并在实施文件中:

Scheduler * Scheduler::_singleton = 0;

这篇关于 - 变量警告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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