I need to write a simple log class that will write output to a file.
I want it to work with operator overloading, 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 do not 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?
source
share