#include <iostream>
#include <fcntl.h>
#include <fstream>
using namespace std;
class Logger
{
private:
ofstream debug;
Logger()
{
debug.open("debug.txt");
}
static Logger log;
public:
static Logger getLogger()
{
return log;
}
void writeToFile(const char *data)
{
debug << data;
}
void close()
{
debug.close();
}
};
Logger Logger::log;
Through this class i m trying to create a Logger class which logs into a file. But it gives error like
error: ‘std::ios_base::ios_base(const std::ios_base&)’ is private
i googled it and found that its because of copying ofstreams. As far as i understand in this code no copying of ofstreams is taking place.
Can u guys help me out. Thanks in advance.
~
attempts to return a
Logger
by value, which requires a copy-constructor. The compiler-generated copy-constructor attempts to make a copy of the memberdebug
. Which is why you get the error.You can either implement a copy constructor (probably doesn't make sense, since the
debug
member would be different) or return by reference:which is safe in this case, since
log
has static storage duration.A correct call would look like:
in which case
l
refers toLogger::log
.