2015-06-22 76 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文件

相关问题