Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to initialize a static member

I want to initialize two static data members. See the two files

// Logger.h   

class Logger
{
    public:
        static LoggerConcrete error;
        static LoggerConcrete write;
};

and

//Logger.cpp

Logger::error = LoggerConcrete(LOG_DEBUG);
Logger::write = LoggerConcrete(LOG_DEBUG);

The initilization of the two static members in Logger.cpp doesn't work. I get the following compiler (g++) error:

g++ -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"Logger.d" -MT"Logger.d" -o"Logger.o" "../Logger.cpp" ../Logger.cpp:13: error: expected constructor, destructor, or type conversion before '=' token ../Logger.cpp:14: error: expected constructor, destructor, or type conversion before '=' token

I want to use Logger::write and Logger::error in each class in which I include Logger.h. How do I have to initialize these two members?

like image 520
Maus Avatar asked Dec 07 '22 06:12

Maus


2 Answers

You need to specify the type:

LoggerConcrete Logger::error = LoggerConcrete(LOG_DEBUG);
LoggerConcrete Logger::write = LoggerConcrete(LOG_DEBUG);
like image 144
Shog9 Avatar answered Jan 02 '23 05:01

Shog9


You can also use this syntax, a little more concise:

LoggerConcrete Logger::error(LOG_DEBUG);
LoggerConcrete Logger::write(LOG_DEBUG);
like image 44
AshleysBrain Avatar answered Jan 02 '23 04:01

AshleysBrain