I have been playing around with Template functions, and made a little logger program. I have been trying to split this into header / source file, but I keep getting linker errors. I know this is simple, but I cant figure it out.
Also I have some convince vars in the logger header, where would be the "proper" place for those? (logStart, logEnd etc.)
All code below.
Header
#ifndef __moot_logger_hpp__
#define __moot_logger_hpp__
#include <iostream>
#include <fstream>
#include <string>
using std::ios;
namespace Moot
{
struct Logger
{
Logger();
~Logger() {}
template <class T>
void saveToFile(T type);
template <typename T>
Logger& operator<< (T type);
/*
Logger& operator<< (std::wstring wideStr)
{
saveToFile(Moot::convertWstringToString(wideStr));
return *this;
}
*/
};
/*
namespace {
Logger logStart;
Logger& lStart = logStart;
const char logEnd = '\n';
const char& lEnd = logEnd;
const char logSpace = ' ';
const char& lSpace = logSpace;
}
*/
}
#endif
Source
#include <Moot/logger.hpp>
Moot::Logger::Logger()
{
std::ofstream logfile;
logfile.open ("logfile.txt", ios::trunc);
logfile << "LogFile - most recent at the bottom\n";
logfile << "-----------------------------------\n \n";
logfile.close();
}
template <typename T>
void Moot::Logger::saveToFile(T type)
{
std::ofstream logfile;
logfile.open ("logfile.txt", ios::in | ios::app);
logfile << type;
logfile.close();
}
template <typename T>
Moot::Logger& Moot::Logger::operator<< (T type)
{
saveToFile(type);
return *this;
}
/*
Logger::Logger& operator<< (std::wstring wideStr)
{
saveToFile(Moot::convertWstringToString(wideStr));
return *this;
}
*/
main
#include <Moot/logger.hpp>
#include <iostream>
int main()
{
std::cout << "hello" << std::endl;
Moot::Logger() << 12;
//Moot::lStart.saveToFile(23);
//Moot::lStart.operator << 13;
return 0;
};