views:

450

answers:

6

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?

A: 

You try to output a char* :

log<<"Message";

But only defined your operator for int :

MyLog& MyLog::operator<<(MyLog& l,int i)

I guess the error message also says "or there is no acceptable conversion"

siukurnin
+14  A: 

You have declared the free function MyLog& operator<<(MyLog& l,char* msg) to be a friend of the MyLog class. It is not a member of the class itself, so your definition of the function should start with this:

MyLog& operator<<(MyLog& l,char* msg)
{
   //...
Charles Bailey
Thanks, it works :)
Ove
Please add the operator<< to the class itself. Do not use friend.Why use friend instead of implementing the method as a member.
Totonga
@Totonga: As a general rule binary operators are better as free functions as it allows them to be found even where the "first" operand of the expression is not the class type. For a log class this is unlikely to be the case, however, for consistency of style it's not a bad thing to implement it in this way.
Richard Corden
Assuming that the intention is to change the function to write to the private member logfile, I agree with Totonga that this doesn't need to be a friend, and therefore should not be. Examples of when you need a function to be friend are: if you want to allow the class to be the second element or if you want to allow promotion of the first element, see http://www.parashift.com/c++-faq-lite/friends.html#faq-14.5As written, though the function doesn't even need to be a member function. :)
Geerad
@Geerad: Why make it a member, if it doesn't have to be?
jalf
A: 

Here you declared the << operator for int, and you are trying to pass a char*.

Try declaring the char* overload as well:

MyLog& operator<<(MyLog& l,char* msg);

By the way, why are you declaring it as friend?

Paolo Tedesco
Sorry, I actually made a function for char* and one for int, and I pasted the one for int because it was the simplest (i didn't want to paste all the code) and I left the log<<"Message" example.Changing it to char* didn't fix it.
Ove
A: 

Try modify

friend MyLog& operator<<(MyLog& l,int i);

to

MyLog& operator<<(MyLog& l,int i);
yoco
A: 
Patrick
+2  A: 

Visual C++ is right, your operator<< is indeed not a member of class MyLog. Try making it a member function instead of a friended separate function:

class MyLog {
 // ...

public:
 MyLog& operator<<(int i);
}

MyLog& MyLog::operator<<(int i) {
 cout << i;
 return *this;
}
Jesper