简体   繁体   English

使用c ++ / QT创建者从另一个类中以及从另一个类中设置和获取Singleton变量

[英]Set and get Singleton variables from and in another class with c++ / QT creator

im new in c++ 我是C ++中的新手

my problem: this is my singleton: 我的问题:这是我的单身汉:

class Singleton
{
    static Singleton *singletonInstance;
    Singleton() {}

    public:
    int numero = 0;

    static Singleton* getSingletonInstance()
    {
        //std::lock_guard<std::mutex> lock(m_);
        if(singletonInstance == nullptr)
        {
            singletonInstance = new Singleton();
        }
        return singletonInstance;
    }
};

this is another file .cpp where i set or get the "numero" variable: 这是另一个文件.cpp,我在其中设置或获取“ numero”变量:

Singleton.getSingletonInstance()->numero = 10;

I get this error: 我收到此错误:

error: expected unqualified-id before '.' 错误:“。”之前的预期unqualified-id。 token 代币

Singleton.getSingletonInstance()->numero = 10; Singleton.getSingletonInstance()-> numero = 10;

How do to set "numero" variable and get/set the numero from sigleton in/from other class .cpp ? 如何设置“ numero”变量并从其他类.cpp中的sigleton获取/设置numero? My target is use this singleton. 我的目标是使用此单例。 I'm new in c++. 我是C ++的新手。 Where I wrong ? 我哪里错了? Thanks 谢谢

The symbol Singleton is not an object, it's a class. 符号Singleton不是对象,而是类。 For that you have to use the scoping operator :: : 为此,您必须使用作用域运算符 ::

Singleton::getSingletonInstance()->numero = 10;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM