Hello. I have to write a simple log class, that would write the output to a file.
I want it to work with overloading the << operator, so I can do this:
MyLog log("C:\\log.txt");
log<<"Message";
But Visual C++ tells me: "error C2039: '<<' : is not a member of 'MyLog' "
I don't know what I am doing wrong.
Here is the code:
MyLog.h
#pragma once
#include <iostream>
#include <conio.h>
#include <fstream>
using namespace std;
class MyLog
{
private:
ofstream logfile;
public:
MyLog(char* filename);
friend MyLog& operator<<(MyLog& l,char*msg);
};
MyLog.cpp
#include "MyLog.h"
MyLog::MyLog(char* filename)
{
logfile.open(filename);
}
MyLog& MyLog::operator<<(MyLog& l,char*msg)
{
cout<<msg;
return l;
}
Does anyone know what is wrong?