3
鏈接錯誤,我需要生成一個簡單的記錄器(用了第三方庫),這是必須鏈接DLL。因此,我生成了一些他的類方法的類是模板,代碼編譯正常,但我得到鏈接器錯誤。我正在編譯MS VS 2008和gcc-4。該代碼是:在C++模板
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
請注意,模板代碼工作就好了;你的鏈接器錯誤是在*非* -template代碼。 – 2012-04-24 15:54:43