I'm pretty new to c++ and I need your help, I think it's pretty easy questions but i cant get over the error
I have a .h file which includes methods to log error in txt file given but actually I dont really know how to use it could you please help me ? I try to test in the main by wrinting some error but i cant use the operator "<<"
here is the .h file
class FileLogger {
public:
// If you can´t/dont-want-to use C++11, remove the "class" word after enum
enum e_logType { LOG_ERROR, LOG_WARNING, LOG_INFO };
// ctor (remove parameters if you don´t need them)
explicit FileLogger (const char *engine_version, const char *fname = "log.txt")
: numWarnings (0U),
numErrors (0U)
{
myFile.open (fname);
// Write the first lines
if (myFile.is_open()) {
myFile << "Engine, version " << engine_version << std::endl;
myFile << "Log file created" << std::endl << std::endl;
} // if
}
// dtor
~FileLogger () {
if (myFile.is_open()) {
myFile << std::endl << std::endl;
// Report number of errors and warnings
myFile << numWarnings << " warnings" << std::endl;
myFile << numErrors << " errors" << std::endl;
myFile.close();
} // if
}
// Overload << operator using log type
friend FileLogger &operator << (FileLogger &logger, const e_logType l_type) {
switch (l_type) {
case FileLogger::e_logType::LOG_ERROR:
logger.myFile << "[ERROR]: ";
++logger.numErrors;
break;
case FileLogger::e_logType::LOG_WARNING:
logger.myFile << "[WARNING]: ";
++logger.numWarnings;
break;
default:
logger.myFile << "[INFO]: ";
break;
} // sw
return logger;
}
// Overload << operator using C style strings
// No need for std::string objects here
friend FileLogger &operator << (FileLogger &logger, const char *text) {
logger.myFile << text << std::endl;
return logger;
}
// Make it Non Copyable (or you can inherit from sf::NonCopyable if you want)
FileLogger (const FileLogger &) = delete;
FileLogger &operator= (const FileLogger &) = delete;
private:
std::ofstream myFile;
unsigned int numWarnings;
unsigned int numErrors;
}; // class end
#endif // FILELOGGER_HPP
and the main
#include <iostream>
#include <fstream>
#include <vector>
#include <tuple>
#include <mutex>
#include <chrono>
#include <ctime>
#include "Log_File.h"
int main ()
{
FileLogger test("oke", "testloginfo" );
test<<(test, 'LOG_ERROR'); // HERE I got a problem i just want to write an error
};
The error I got is strange , it's "error:ambiguous overload for 'operator<<' operand types are 'FileLogger' and 'int'"
Thank you!