error: expected unqualified-id before ‘.’ token

C++ 为了实现加锁的功能,引入了mutex头文件,使用mutext.lock()上锁,mutex.unlock()释放锁。

#include 

using namespace std;

mutex t_mutex;

class Csingleob
{
        private:
                Csingleob(){}
        static Csingleob *p;

        public:
                static Csingleob* getInstance()
                {
                        mutex.lock();
                        if (p == NULL)
                        {
                                p = new Csingleob();
                        }
                        mutex.unlock();
                        return p;
                }
};

但是,在编译的时候报错,提示:

error: expected unqualified-id before ‘.’ token    mutex.lock();

 直接使用mutext.lock()上锁,mutex.unlock()释放锁是错误的,应当先对类或者结构体实例化,再用“.”调用相应的方法;

修改之后为

#include 

using namespace std;

mutex t_mutex;

class Csingleob
{
        private:
                Csingleob(){}
        static Csingleob *p;

        public:
                static Csingleob* getInstance()
                {
                        t_mutex.lock();
                        if (p == NULL)
                        {
                                p = new Csingleob();
                        }
                        t_mutex.unlock();
                        return p;
                }
};

Csingleob* Csingleob::p = NULL;

 

 

 

 


 

你可能感兴趣的:(C++)