2015-06-22 80 views
3

我在我的調試代碼中完成了自己的printf()(經典)重新實現。將可變參數模板參數包傳遞給下一個函數

template<typename T, typename ...Args> 
    void Printf(wchar_t const * message, T value, Args ...args); 
    void Printf(wchar_t const * message); 
    void Printf(); 

它使用可變模板並且工作得很好。

現在我想在幾個函數中使用Printf(),它們將接受幾乎相同的參數,但以不同的方式執行Printf()。下面是對printf的)客戶端功能(

template<typename ...Args> 
void Debug::Line(unsigned int in_tabs, wchar_t const * in_string, Args...in_args){ 
    for (unsigned int i = 0; i < in_tabs; ++i) 
     Tab(); 
    Printf(in_string, in_args...); 
    NewLine(); 
} 

一旦我開始使用這個結構之一,我開始變得UNRESOLVED連接錯誤

error LNK2019: unresolved external symbol "public: static void __cdecl Nerv::Debug::Line<>(unsigned int,wchar_t *)" ([email protected][email protected]@[email protected]@[email protected]) referenced in function "public: void __thiscall Nerv::UI::NervUIRect::DebugRect(void)" ([email protected]@[email protected]@@QAEXXZ) 
error LNK2019: unresolved external symbol "public: static void __cdecl Nerv::Debug::Line<int,int>(unsigned int,wchar_t *,int,int)" ([email protected]@[email protected]@@[email protected]) referenced in function "public: void __thiscall Nerv::UI::NervUIRect::DebugRect(void)" ([email protected]@[email protected]@@QAEXXZ) 

很奇怪我的,因爲我已經測試過這首先構建一個更簡單的案例,它工作得很好。

template<typename T,typename...Ts> 
void Printf(T value,Ts...args){ 
    OutputDebugString(value); 
    Printf(args...); 
} 
void Printf(){ 

} 

template<typename...Args> 
void UsePrintf(Args...args){ 
    Printf(args...); 
} 

從簡單的情況不同的是,所有theese功能(即不工作)是一個Debug類的靜態成員,並且還有一些額外的功能參數。 那麼我該如何處理這個問題呢?

+2

頭文件中定義了「Debug :: Line」嗎? – TartanLlama

+0

是的,它聲明如下\t \t static void Line(unsigned int in_tabs,wchar_t * in_string,Args ... in_args); – Antiusninja

+5

我不是指聲明,我的意思是定義。模板[必須在頭文件中實現](http://stackoverflow.com/questions/495021/why-can-templates-only-be-implemented-in-the-header-file)。 – TartanLlama

回答

0

作爲TartanLlama指出可用所需要的模板函數體/看出在頭文件或者通過將定義和聲明togather在報頭或通過包括在報頭中的一端與功能執行cpp文件

相關問題