Normally you put class definitions in a header file and method definitions in a source file. Code that creates or uses objects of the class #includes the header file and obtains access to the method code via the linker.
But the Template files can not organized like the non-template files. If you define the template class by organizing the declaration file (.h) and the definition file (.cpp) Normally. You will get some linking errors after building the project.

But why templates don't work this way? 

The reason is clear:

When the compiler encounters template method definitions, it performs syntax checking, but doesn't actually compile the templates. 
When the compiler encounters an instantiation of the template, such as Ruler<int> myRuler, it writes code for an int version of the  Ruler template by replacing each T in the template class definition with int. 
So, if you don't instantiate a class template for any types in your program, then the class method definitions are never compiled.

Then how to organize the Template Files? 
There are 3 methods listed below can be used.
1.
You can place the method definitions directly in the same header file where you define the class itself. When you #include this file in a source file where you use the template, the compiler will have access to all the code it needs.

2.
You can place the template method definitions in a 
separate header file that you #include in the header file with the class definitions. Make sure the #include for the method definitions follows the class definition; otherwise the code won't compile.
But method implementations look strange in header files. 

template <typename T>
class Ruler
{
    // Class definition omitted for brevity
};
#include " Ruler_Def.h"


3.
You can do by #include the method implementation source file(.cpp) in the template class definition header file, must follows the class definition.

template <typename T>
class Ruler
{
    // Class definition omitted for brevity
};
#include " Ruler.cpp"


When using this technique, make sure you don't add the  Ruler.cpp file to your project, because it is not supposed to be, and cannot be compiled separately; it should only be #included in a header file.

You can actually call your file with method implementations anything you want. Some programmers like to give source files that are included an .inl extension.


How to organize the Template Files in C++的更多相关文章

  1. Ace - Responsive Admin Template

    Ace简介: Ace 是一个轻量.功能丰富.HTML5.响应式.支持手机及平板电脑上浏览的管理后台模板,基于CSS框架Bootstrap制作,Bootstrap版本更新至 3.0,Ace – Resp ...

  2. [转] How to generate multiple outputs from single T4 template (T4 输出多个文件)

    本文转自:http://www.olegsych.com/2008/03/how-to-generate-multiple-outputs-from-single-t4-template/ Updat ...

  3. go标准库的学习-text/template

    参考:https://studygolang.com/pkgdoc 导入方式: import "text/template" template包实现了数据驱动的用于生成文本输出的模 ...

  4. How To Use XDOLoader to Manage, Download and Upload Files? (文档 ID 469585.1)

    Applies to: BI Publisher (formerly XML Publisher) - Version 5.6.3 to 5.6.3 [Release 5] Information  ...

  5. How To Use XDOLoader to Manage, Download and Upload Files? (DOC ID 469585.1)

    In this Document Goal Fix     Downloading Files   Uploading Files References Applies to: BI Publishe ...

  6. XML Publiser For Excel Template

    1.XML Publisher定义数据 2.XML Publisher定义模板 模板类型选择Microsoft Excel,默认输出类型选择Excel,上传.xls模板 3.定义并发程序 4.定义请求 ...

  7. [TypeStyle] Generate static css + html files using TypeStyle

    You can easily use TypeStyle to build static html files with encapsulated CSS. You can use this patt ...

  8. 如何使用 C++ Inja html template 模板

    C++ html template Inja是现代C ++的模板引擎,受到jinja for python的启发.它有一个简单而强大的模板语法,包含所有变量,循环,条件,包含,回调,您需要的注释,嵌套 ...

  9. Getting Started with ASP.NET Web API 2 (C#)

    By Mike Wasson|last updated May 28, 2015 7556 of 8454 people found this helpful Print   Download Com ...

随机推荐

  1. 函数get_table_share

    得到一个table_share 1)先从table_def_cache中查找, 如果有, 直接返回 2)如果没有找到,    为table_share分配内存,初始化,打开.frm文件,并将share ...

  2. bzoj3413

    SAM好题,显然我们不能与每个后缀都去算LCP 考虑对询问串每一位算贡献,先构建出逆序构建自动机,这样我们得到了原串的后缀树(parent树) 根据parent树的定义,一个节点对应字符串出现的位置对 ...

  3. UVa 11526 H(n)

    题意: long long H(int n){ long long res = 0; for( int i = 1; i <= n; i=i+1 ){ res = (res + n/i); } ...

  4. lrj计算几何模板

    整理了一下大白书上的计算几何模板. #include <cstdio> #include <algorithm> #include <cmath> #include ...

  5. HDU 3467 (求五个圆相交面积) Song of the Siren

    还没开始写题解我就已经内牛满面了,从晚饭搞到现在,WA得我都快哭了呢 题意: 在DotA中,你现在1V5,但是你的英雄有一个半径为r的眩晕技能,已知敌方五个英雄的坐标,问能否将该技能投放到一个合适的位 ...

  6. andeoid学习笔记七

    Android中Broadcast的Intent大全 Api Level 3:(SDK 1.5) android.bluetooth.a2dp.intent.action.SINK_STATE_CHA ...

  7. Linux系统性能监控

    系统的性能指标主要包括CPU.内存.磁盘I/O.网络几个方面. 1. CPU性能 (1)利用vmstat命令监控系统CPU 该命令可以显示关于系统各种资源之间相关性能的简要信息,这里我们主要用它来看C ...

  8. 定义页面的Dispose方法:[before]unload事件启示录

    前言 最近实施的同事报障,说用户审批流程后直接关闭浏览器,操作十余次后系统就报用户会话数超过上限,咨询4A同事后得知登陆后需要显式调用登出API才能清理4A端,否则必然会超出会话上限. 即使在页面上增 ...

  9. Perfect Service

    题意: n个节点树,在一个节点放上一台服务器可以给相邻的其他各点提供服务且一个节点只能接受一台服务器,求使n个节点都被服务放的服务器的最小数量. 分析: 不算太难,状态想的差不多,但是考虑不全面状态方 ...

  10. RPC框架motan: 通信框架netty之Netty4Client

    上文已经初步探讨了如何实现一个具体的transport,本文就来讨论一个具体的transport,本文讨论netty4的的相关实现.老规矩,看看motan-transport的目录结构. 其中最重要的 ...