How to separate the implementation and definition for template function in c++

简介:

When I use template function in my class TestClass. The complier will throw out a Link Error below.

TestClass.h 

template<class T>    
T foo(T a, T b);

TestClass.cpp 
   

template<class T>    
T TestClass::foo(T a, T b)
{
return a;
}

Client.cpp 

TestClass testClass;
testClass.foo(0, 1);

Link Error 

Error    1    error LNK2019: unresolved external symbol "public: int __cdecl TestClass::foo<int>(int,int)" (??$foo@H@TestClass@@QAAHHH@Z) referenced in function "private: bool __cdecl Client::TestFunction()" (?TestFunction@Client@@@Z)    Client.obj    

There are some approach we can use to fix this issue. I prefer to use the implementation header to fix that issue. 
Add a new file called TestClass_impl.h and move all the template functions from TestClass.cpp to TestClass_impl.h

Include TestClass_impl.h into TestClass.h

TestClass.h 

class TestClass
{
template<class T>
T foo(T a, T b);
};
#include "TestClass_impl.h"

All done.



    本文转自Jake Lin博客园博客,原文链接:http://www.cnblogs.com/procoder/archive/2010/06/29/How-to-Separate-the-Implementation-and-Definition-of-Template-Function-in-CPP.html,如需转载请自行联系原作者


相关文章
Matlab:成功解决Function definition are not permitted at the prompt or scripts
Matlab:成功解决Function definition are not permitted at the prompt or scripts
definition of dllimport function not allowed 错误
definition of dllimport function not allowed 不允许dllimport函数的定义 本应该是导出,结果写成导入了,就会出现这个问题 改为:dllexport 问题解决。
873 0
|
2月前
【Azure Durable Function】PowerShell Activity 函数遇见 Newtonsoft.Json.JsonReaderException: The reader's MaxDepth of 64 has been exceeded.
【Azure Durable Function】PowerShell Activity 函数遇见 Newtonsoft.Json.JsonReaderException: The reader's MaxDepth of 64 has been exceeded.
|
2月前
|
安全 JavaScript 应用服务中间件
【Azure Function App】如何修改Azure函数应用的默认页面呢?
【Azure Function App】如何修改Azure函数应用的默认页面呢?
|
2月前
|
C# C++ Python
【Azure 应用服务】Azure Durable Function(持久函数)在执行Activity Function时候,因为调用函数名称错误而导致长时间无响应问题
【Azure 应用服务】Azure Durable Function(持久函数)在执行Activity Function时候,因为调用函数名称错误而导致长时间无响应问题
|
2月前
|
SQL JavaScript 前端开发
【Azure 应用服务】Azure JS Function 异步方法中执行SQL查询后,Callback函数中日志无法输出问题
【Azure 应用服务】Azure JS Function 异步方法中执行SQL查询后,Callback函数中日志无法输出问题
|
2月前
|
JSON 数据格式 Python
【Azure 应用服务】Azure Function Python函数中,如何获取Event Hub Trigger的消息Event所属于的PartitionID呢?
【Azure 应用服务】Azure Function Python函数中,如何获取Event Hub Trigger的消息Event所属于的PartitionID呢?
|
2月前
|
C++ Python
【Azure 应用服务】Azure Function Python函数部署到Azure后遇见 Value cannot be null. (Parameter 'receiverConnectionString') 错误
【Azure 应用服务】Azure Function Python函数部署到Azure后遇见 Value cannot be null. (Parameter 'receiverConnectionString') 错误
|
2月前
【Azure 应用服务】Azure Function Timer触发函数加上Singleton后的问题
【Azure 应用服务】Azure Function Timer触发函数加上Singleton后的问题
|
2月前
|
消息中间件 Kafka 网络安全
【Azure 应用服务】本地创建Azure Function Kafka Trigger 函数和Kafka output的HTTP Trigger函数实验
【Azure 应用服务】本地创建Azure Function Kafka Trigger 函数和Kafka output的HTTP Trigger函数实验

热门文章

最新文章

下一篇
无影云桌面