Click here to Skip to main content
15,903,175 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
{
//private:
      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"
//#include <iostream>
//#include <fstream>
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:
// LibVault_Tester.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "LibVault_Tester.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
// The one and only application object
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 
{
//private:
      //fstream *logfile;
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;//<<"\r\nIt Works\r\n"<<5.6;
}
</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].
Posted

1 solution

1. Why do you define a second Logger in tester.cpp ? :)
0. Try to use (test) the operator in the DLL code firstly :)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900