I have this class:
Logger.h
class Logger{
std::string filename;
std::ofstream fileStream;
/*Some methods ...*/
};
extern Logger* log;
In another header file i have included the *log definition:
Foo.h
#include "Logger.h"
class Foo{
Logger* log;
/*Other code*/
};
But if in Foo.cpp file i try to do this:
log = new Logger();
the compiler give me an error, any idea?
First, variables declared as
externalmust be defined. So you need to havein
Logger.cpp. You can also initialize it there like this:Second, you don’t need any more declarations, that is you just need to include
Logger.h, no need to declare anotherLoggervariable inFoo.h, just uselogfromLogger.h.