Static member error in Singleton implementation

1

When implementing the Singleton pattern the compiler gives the following error:

  

include \ graphdata.h | 21 | error: 'constexpr' needed for in-class initialization of static data member 'graphdata * graphdata :: instance' of non-integral type [-fpermissive] |

The code I made is as follows:

class graphdata
{
    public:
        static graphdata& getinstance(){
            if(!instance)
                instance = new graphdata();
            return *instance;
        }
        void dfsR();
        graphdata(graphdata const&) = delete;
        void operator = (graphdata const&) = delete;
    protected:

    private:
        graphdata();
        static graphdata* instance = 0;
};
    
asked by anonymous 05.10.2018 / 16:20

1 answer

0

This is exactly what is written, if you want to initialize a value in the class declaration it must be a constant expression ( constexpr ). If it can not be constant you should boot into the static constructor or your normal code before using the variable.

    
05.10.2018 / 16:33