I am trying to write an API (called logEvent) for an outdated os-level logging function. I would like to call it in the following manner.
logEvent << "string";
// or
logEvent << "string" << 123 << 0.456 << ... ;
I'm trying to accomplish this by overloading the stream operator, and making it take a template argument so it will pass it to the outdated logging function. I have tried befriending ostream, but I can't get it to work either way, nor do I really understand why I need to do that.
The way I have it right now, I get the following linker error, but only when I call the overloaded operator from main.cpp (below).
main.obj : error LNK2019: unresolved external symbol "public: class logEvent * __thiscall logEvent::operator<<<int>(int)" (??$?6H@logEvent@@QAEPAV0@H@Z) referenced in function _main
I have been up and down the web (including this forum) and have not been able to find a solution. Any help is greatly appreciated.
// main.cpp
#include "logEvent.h"
using namespace std;
int main()
{
logEvent e;
e << 123;
return 0;
}
// logEvent.h
#include <string>
#include <sstream>
class logEvent
{
public:
logEvent();
template< typename T >
logEvent *operator<<( T arg );
}
// logEvent.cpp
#include <string>
#include <sstream>
template< typename T >
logEvent *logEvent::operator<<( T arg )
{
oldLog( arg );
return this;
}