R
Riku Jarvinen
Hello everyone,
I have a logging class which writes program outputs to the logfile. The
class works fine as long as only C++ native data types are considered. The
problem is that I have a program with a bunch of classes which have the
output stream operators << of their own. The overloaded operators work fine
when inserted into the std:cout stream but I don't know how to make the
logging class accept them.
If I try to compile the program (see the code below) with the command
mainlog << a;
included I get the following error message:
logtest.cpp: In function `int main()':
logtest.cpp:64: error: no match for 'operator<<' in 'mainlog << a'
logtest.cpp:20: error: candidates are: Logger& Logger:
perator<<(const
char*)
logtest.cpp:25: error: Logger& Logger:
perator<<(int)
logtest.cpp:46: error: std:
stream&
operator<<(std:
stream&,
const MyClass&)
Does this mean I have to somehow introduce the overloaded operator << of
MyClass (and every other class I use in my program) separately in the Logger
class?
Here follows the simplified example:
------------------------------------
#include <iostream>
#include <fstream>
using namespace std;
class Logger
{
public:
Logger(const char* filename = "program.log")
{
logfile = new fstream(filename, fstream:
ut);
}
~Logger()
{
logfile->close();
delete logfile;
}
Logger& operator<<(const char* msg)
{
*logfile << msg;
return *this;
}
Logger& operator<<(const int val)
{
*logfile << val;
return *this;
}
private:
fstream* logfile;
};
class MyClass
{
public:
MyClass(const int n)
{
data = n;
}
~MyClass()
{
}
friend ostream& operator<<(ostream& o, const MyClass& v)
{
return o << "MyClass data = " << v.data << "\n";
}
private:
int data;
};
int main()
{
Logger mainlog;
MyClass a(10);
// These work fine
cout << a;
mainlog << "Bla " << 200 << "\n";
// This doesn't work
//mainlog << a;
return 0;
}
I have a logging class which writes program outputs to the logfile. The
class works fine as long as only C++ native data types are considered. The
problem is that I have a program with a bunch of classes which have the
output stream operators << of their own. The overloaded operators work fine
when inserted into the std:cout stream but I don't know how to make the
logging class accept them.
If I try to compile the program (see the code below) with the command
mainlog << a;
included I get the following error message:
g++ logtest.cpp
logtest.cpp: In function `int main()':
logtest.cpp:64: error: no match for 'operator<<' in 'mainlog << a'
logtest.cpp:20: error: candidates are: Logger& Logger:
char*)
logtest.cpp:25: error: Logger& Logger:
logtest.cpp:46: error: std:
operator<<(std:
const MyClass&)
Does this mean I have to somehow introduce the overloaded operator << of
MyClass (and every other class I use in my program) separately in the Logger
class?
Here follows the simplified example:
------------------------------------
#include <iostream>
#include <fstream>
using namespace std;
class Logger
{
public:
Logger(const char* filename = "program.log")
{
logfile = new fstream(filename, fstream:
}
~Logger()
{
logfile->close();
delete logfile;
}
Logger& operator<<(const char* msg)
{
*logfile << msg;
return *this;
}
Logger& operator<<(const int val)
{
*logfile << val;
return *this;
}
private:
fstream* logfile;
};
class MyClass
{
public:
MyClass(const int n)
{
data = n;
}
~MyClass()
{
}
friend ostream& operator<<(ostream& o, const MyClass& v)
{
return o << "MyClass data = " << v.data << "\n";
}
private:
int data;
};
int main()
{
Logger mainlog;
MyClass a(10);
// These work fine
cout << a;
mainlog << "Bla " << 200 << "\n";
// This doesn't work
//mainlog << a;
return 0;
}