I am writing a Logger and for this i want to overload the operator<<() to Log strings and numbers...
I encounter problems which make me want to ask the following:
If I want to do: logger << string e.g.: log << "lalelu";
do i have to overload the << for std::string,too? or is it sufficient to do the following:
1 2 3 4 5
class Logger
{
//...
Logger& operator<<(std::string) {/*do stuff*/ returnthis;};
};
i am asking, because i get the following error(s): c2678
binary 'operator' : no operator defined which takes a left-hand operand of type 'type' (or there is no acceptable conversion)
& c2784
error C2784: "std::basic_ostream<_Elem,_Traits> &std::operator <<(std::basic_ostream<_Elem,_Traits> &,_Elem)": template-Argument für "std::basic_ostream<_Elem,_Traits> &"can not be deduced from 'Logger'
#include <streambuf>
#include <iostream>
#include <cstdio>
class LogBuf : public std::streambuf {
protected:
virtual
int_type overflow(int_type c) {
if (c == EOF)
return EOF;
int rv1 = write_char_func1(ch);
int rv2 = write_char_func2(ch);
// your error checking
if (rv1 == ERROR || rv2 == ERROR)
return EOF;
return 1;
}
// this is optional, but due to performance i recommend to write this too
virtual
std::streamsize xsputn(constchar *buf, std::streamsize num) {
int rv1 = write_cstring_func1(buf, num);
int rv2 = write_cstring_func2(buf, num);
// your error checking
if (rv1 == ERROR || rv2 == ERROR)
return EOF;
// please make somehow sure to write num chars... ;-)
// normally you would return how many chars could be written
// same in overflow
return num;
}
};
class LogStream : public std::ostream {
private:
LogBuf buf;
public:
explicit LogStream() : std::ostream(buf) { }
};
The problem is just what the error is saying. In the scope of your DO function the << operator was not known. Just do an include "logger.h" in your winsockmanager.cpp.
I don't but I have an idea. Build a small project that compiles only the logger class and a small main function that attempts to log a few strings. Does that compile? You've only given us small pieces of code to look at so there is no way for me to figure out why you are getting a linker error. I cannot see how the project is being built. Also I have no idea if that is the only linker error that you get. Post something that I can compile and I'll try it or someone else will.