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. hdu 3359 Kind of a Blur (高斯消元 浮点型)

    题目链接 题意: H * W (W,H <= 10) 的矩阵A的某个元素A[i][j],从它出发到其他点的曼哈顿距离小于等于D的所有值的和S[i][j]除上可达点的数目,构成了矩阵B.给定矩阵B ...

  2. 用net匹配并替换iOS标准的emoji表情符号

    using System; using System.Collections.Generic; using System.Linq; using System.Text; namespaceCommo ...

  3. Android基础_2 Activity线性布局和表格布局

    在activity的布局中,线性布局和表格布局是最简单的,这次分别从线性布局,表格布局以及线性布局和表格混合布局做了实验,实验中只需要编写 相应的xml的代码,java代码不需要更改,因为我们这里只是 ...

  4. Spring下载

    Spring官网改版后找了好久都没有找到直接下载Jar包的链接,下面汇总些网上提供的方法,亲测可用. 1.直接输入地址,改相应版本即可:http://repo.springsource.org/lib ...

  5. SpringMVC——注解的使用与结果跳转方式

    1.项目结构 2.源代码 package com.zhengbin.controller; import java.io.IOException; import javax.servlet.Servl ...

  6. 连接Excel时出现未指定的错误

    使用  strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filepath + ";Extended ...

  7. android bin目录下的.ap_是神马文件?

    resources.ap_ resources翻译过来是资源的意思 应该就是一种中间文件,可以改成rar.zip等压缩文件的类型,里面包含res.AndroidMainfest.xml.resourc ...

  8. wifi详解(五)

    1        Android平台的Wifi模块移植要点 1.1      Wifi结构 user interface Android WiFiService WPA_Supplicant DHD ...

  9. [Papers]NSE, $\p_3u$, Lebesgue space [Cao, DCDSA, 2010]

    $$\bex \p_3\bbu\in L^p(0,T;L^q(\bbR^3)),\quad \frac{2}{p}+\frac{3}{q}=2,\quad \frac{27}{16}\leq q\le ...

  10. T-SQL 数据库笔试题

    1.说明:创建数据库 Create DATABASE database-name 2.说明:删除数据库 drop database dbname 3.说明:备份sql server --- 创建备份数 ...