我需要生成一个需要链接到 dll 的简单记录器(没有第三方库)。因此我用他的一些类方法生成了一个类是模板,代码编译正常但我得到链接器错误。我正在使用 MS VS 2008 和 gcc-4 进行编译。代码是:
Log.h 类:
class MiniLogger
{
private:
   std::ofstream mOutPutFile;
protected:
   void write (std::string const& text);
public:
   ~MiniLogger();
   MiniLogger( std::string const& lName) throw(FileError);
   static MiniLogger* getInstance(std::string const & fName);
   static void destoryInstance(MiniLogger*);
  template<class T>
  MiniLogger& operator<<(T & data);
  MiniLogger& operator<<(std::ostream& (*func)(std::ostream&) );
};
MiniLogger& MiniLogger::operator<< (std::ostream& (*func)(std::ostream&))
{
   //mOutPutFile << out;
   return *this;
}
template<class T>
MiniLogger& MiniLogger::operator<< (T  & data)
{
 //Just try with out calling other method
// write(data);
   return *this;
}
主要我实例化了对象并使用它:
#include "log.h"
int main()
{
    MiniLogger &a=*(MiniLogger::getInstance("text.txt"));
    a << "text" << std::endl;
return 0;
}
我明白了
@ubu11-10-64b-01:~/cplus/template$ g++ main.cpp log.cpp
/tmp/cccMdSBI.o: In function `MiniLogger::operator<<(std::basic_ostream<char, std::char_traits<char> >& (*)(std::basic_ostream<char, std::char_traits<char> >&))':
log.cpp:(.text+0x0): multiple definition of `MiniLogger::operator<<(std::basic_ostream<char, std::char_traits<char> >& (*)(std::basic_ostream<char, std::char_traits<char> >&))'
/tmp/ccj3dfhR.o:main.cpp:(.text+0x0): first defined here