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?
You need to specify the type:
LoggerConcrete Logger::error = LoggerConcrete(LOG_DEBUG);
LoggerConcrete Logger::write = LoggerConcrete(LOG_DEBUG);
You can also use this syntax, a little more concise:
LoggerConcrete Logger::error(LOG_DEBUG);
LoggerConcrete Logger::write(LOG_DEBUG);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With