小编Ank*_*itG的帖子

如何使用不同的线程访问Singleton类成员函数?

我试图在使用单例实例的不同线程上运行print()和print(char ch)方法.

任何人都可以帮我解决为什么我会收到错误: -

错误C3867:'Singleton :: print':函数调用缺少参数列表; 使用'&Singleton :: print'创建指向成员的指针

错误C3867:'Singleton :: print':函数调用缺少参数列表; 使用'&Singleton :: print'创建指向成员的指针

错误C2661:'std :: thread :: thread':没有重载函数需要2个参数

还帮我纠正下面的代码给我.


class Singleton
{
private:
    Singleton()
    {}
    static Singleton* singletonInstance;
public:
    static Singleton* getSingletonInstance();
    void print()
    {
        std::this_thread::sleep_for(std::chrono::milliseconds(500));
        for (int i = 0; i < 100000; i++)
        {
            cout<<i<<endl;
        }
    }
    void print(char ch)
    {
        std::this_thread::sleep_for(std::chrono::milliseconds(500));
        for (int i = 0; i < 100000; i++)
        {
            cout<<ch<<" "<<i<<endl;
        }
    }
};
Singleton* Singleton::singletonInstance = nullptr;
Singleton* Singleton::getSingletonInstance()
{
    if(!singletonInstance) …
Run Code Online (Sandbox Code Playgroud)

c++ oop design-patterns c++11

3
推荐指数
1
解决办法
738
查看次数

标签 统计

c++ ×1

c++11 ×1

design-patterns ×1

oop ×1