Hi All,
Scenario:
I have C++ Dll problem.In a empty dll project, I have a class[Logger] in a dll, That class uses overloading of “<<” operator; I am passing a Templete variable to that overloading operator:
It compiles and creates a dll ok. But when I try to use the overloaded functionality in a exe it is getting some linker error.
Any help?
Or Generally any idea about “Exporting class for a dll ,the class has a member function which is receiving a template variable”?
Logger.h:
#include <fstream>
#include <iostream>
using namespace std;
#ifdef LIBVAULT_EXPORTS
#define LIBVAULT_API __declspec(dllexport)
#else
#define LIBVAULT_API __declspec(dllimport)
#endif
class LIBVAULT_API Logger
{
fstream *logfile;
public:
Logger() ;
virtual ~Logger() ;
bool Initialize(const char *,int);
template<class TLogger>
friend Logger& operator<<(Logger,const TLogger &);
};
Logger.cpp:
#include "StdAfx.h"
#include "Logger.h"
Logger::Logger(void)
{
logfile=NULL;
}
Logger::~Logger(void)
{
if(logfile)
{
logfile.close();
delete logfile;
}
}
bool Logger::Initialize(const char *Filename,int Mode)
{
if(!logfile)
{
logfile = new fstream(Filename, fstream::out);
}
return true;
}
template<class TLogger>
Logger& operator<<(Logger logger,const TLogger & msg)
{
*logfile << msg;
return *this;
}
In the Exe side:
#include "stdafx.h"
#include "LibVault_Tester.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
CWinApp theApp;
using namespace std;
#pragma comment(lib,"..\\Debug\\LibVault.lib")
#ifdef LIBVAULT_EXPORTS
#define LIBVAULT_API __declspec(dllexport)
#else
#define LIBVAULT_API __declspec(dllimport)
#endif
class LIBVAULT_API Logger
{
public:
Logger() ;
virtual ~Logger() ;
bool Initialize(const char *,int);
template<class TLogger>
friend Logger& operator<<(Logger logger,const TLogger & msg);
};
int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
fnLibVault();
Logger m_Inst;
m_Inst.Initialize("Mylog.txt",1);
m_Inst<<8;
}
</pre>
Error:
LNK2019: unresolved external symbol "class Logger & __cdecl operator<<<int>(class Logger,int const &)" (??$?6H@@YAAAVLogger@@V0@ABH@Z) referenced in function _wmain
The code is compiled with VS2008[VC9.0].