VC ++-DLL-导出具有模板成员功能的类 [英] VC++ - Dll - Export a Class which has a Templated Member function

查看:171
本文介绍了VC ++-DLL-导出具有模板成员功能的类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,
场景:
我有C ++ Dll问题.在一个空的dll项目中,我在dll中有一个类[Logger],该类使用<<"的重载操作员;我正在将Templete变量传递给该重载运算符:
它会编译并创建一个dll.但是,当我尝试在exe中使用重载功能时,出现了一些链接器错误.
有帮助吗?
还是一般而言,任何有关为dll导出类,该类都有一个接收模板变量的成员函数"的想法?



Logger.h:

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].

推荐答案

?6H @@ YAAAVLogger @@ V0 @ ABH @ Z)在函数_wmain中引用 该代码是使用VS2008 [VC9.0]编译的.
?6H@@YAAAVLogger@@V0@ABH@Z) referenced in function _wmain The code is compiled with VS2008[VC9.0].


1.为什么要在tester.cpp中定义第二个Logger? :)
0.首先尝试在DLL代码中使用(测试)运算符:)
1. Why do you define a second Logger in tester.cpp ? :)
0. Try to use (test) the operator in the DLL code firstly :)


这篇关于VC ++-DLL-导出具有模板成员功能的类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆