error: expected unqualified-id before ‘.’ token

C++ introduced the mutex header, which USES mutext.lock() to lock and mutex.unlock() to release the lock.

#include <mutex>

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;
                }
};

However, an error is reported at compile time. Prompt:

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

It is an error to use mutext.lock() to lock, and mutex.unlock() to release the lock. You should instantiate the class or structure first, then call the corresponding method with “.”
After modification:

#include <mutex>

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;

 
 
 
 

Read More: