声明和实现在一个文件中:

template<class T>
class book
{
public:
book();
~book(); private: }; template<class T>
book<T>::book()
{
} template<class T>
book<T>::~book()
{
} int main()
{
book<int> b; return 0;
}

  

声明和实现分开在h和cpp文件中:

潜在问题是,仅仅按照普通class的方式分开,在编译的时候,会报 link2019 错误。原因是在编译实例化模板类的文件时,模板类只有声明文件(h)被链接,其实现文件(cpp里面的部分)找不到。

解决办法:

* 1 在实例化模板类的文中开头,包含该 cpp 文件(#include "xxx.cpp")

* 2 声明和实现写在同一个h文件中;(本人不推荐这么做)

------------------- book.h -----------------------------------

#pragma once

template<class T>
class book
{
public:
book();
~book(); private: };

  

------------------- book.cpp -----------------------------------

#include "book.h"

template<class T>
book<T>::book()
{
} template<class T>
book<T>::~book()
{
}

  

------------------- main.cpp -----------------------------------

#include "book.h"
#include "book.cpp" int main()
{
book<int> b; return 0;
}

  

C++ 模板类示例 template class的更多相关文章

  1. 7.2 C++模板类实例化

    参考:http://www.weixueyuan.net/view/6399.html 总结: array < int >表明用int类型来代替模板类中的类参数“T”,编译器会将模板类ar ...

  2. C++模板类的使用

    1.定义模板类 通过类似于下面的语法可以定义一个模板类: template<typename T> class Job : public virtual RefBase { public: ...

  3. 使用模板类导致error LNK2019: 无法解析的外部符号

    原地址 1.定义模板类: template<class T> class Stack {....}; 2.定义模板成员函数: 每个函数头都要以相同的模板声明打头,并将类限定符改成:类名&l ...

  4. C++高级 STL——模板函数、模板类

    1.模板函数 // 定义 template <class T> Max(T &t1, T &t2) { return ((t1 > t2) ? t1 : t2); } ...

  5. C++ - 模板类模板成员函数(member function template)隐式处理(implicit)变化

    模板类模板成员函数(member function template)隐式处理(implicit)变化 本文地址: http://blog.csdn.net/caroline_wendy/articl ...

  6. template <typename T>模板类定义

    #include "stdafx.h"#include "iostream"#include <ctime>using namespace std; ...

  7. 如何在动态链接库dll/so中导出自定义的模板类template class | how to implement a template class with c++ and export in dll/so

    本文首发于个人博客https://kezunlin.me/post/4ec4ae49/,欢迎阅读最新内容! how to implement a template class with c++ and ...

  8. C++学习三 模板类出错总结(Missing template arguments before 'L')

    一.模板类的说明 模板类有一个好处是可以放宽你输入的数据类型. 比如有这样的一个函数: int add(int x, int y) { return x+y; } 这个函数对于int类型的x,y才适合 ...

  9. C++标准模板库Stand Template Library(STL)简介与STL string类

    参考<21天学通C++>第15和16章节,在对宏和模板学习之后,开启对C++实现的标准模板类STL进行简介,同时介绍简单的string类.虽然前面对于vector.deque.list等进 ...

随机推荐

  1. powershell之utf-8编码

    每次启动powershell后输入:chcp 65001

  2. 201777010217-金云馨《面向对象程序设计(Java)》第二周学习总结

    项目 内容 这个作业属于哪个课程 https://www.cnblogs.com/nwnu-daizh/ 这个作业的要求在哪里 https://www.cnblogs.com/nwnu-daizh/p ...

  3. 算法问题实战策略 PICNIC

    下面是另一道搜索题目的解答过程题目是<算法问题实战策略>中的一题oj地址是韩国网站 连接比较慢 https://algospot.com/judge/problem/read/PICNIC ...

  4. python之海龟绘图

    1. 基本功能介绍 在海龟作图中,我们可以编写指令让一个虚拟的(想象中的)海龟在屏幕上来回移动.这个海龟带着一只钢笔,我们可以让海龟无论移动到哪都使用这只钢笔来绘制线条.通过编写代码,以各种很酷的模式 ...

  5. LG1840 Color the Axis 线段树

    菜的人就要写简单题 为了练习手速来写这样一道 珂朵莉树 线段树简单题 没啥可说的,注意修改操作中要判一下 val=0 #include<bits/stdc++.h> using names ...

  6. 修改SQL Server中的计算机名

    安装SQL Server之后,如果修改计算机名会导致登录异常,或者某些功能不能用,例如配置Replication时会提示如下错误: SQL Server replication requires th ...

  7. cartographer 3D运行录制rosbag包

    目录: 1.运行多线激光雷达: 2.运行IMU: 3.录制rosbag包: 4.配置cartographer: 5.查看地图: 1.运行多线激光雷达: 主要是测试雷达是否正在运行,确认雷达点云topi ...

  8. Codeforces Round #598 (Div. 3) E. Yet Another Division Into Teams dp

    E. Yet Another Division Into Teams There are n students at your university. The programming skill of ...

  9. Golang 入门 : channel(通道)

    笔者在<Golang 入门 : 竞争条件>一文中介绍了 Golang 并发编程中需要面对的竞争条件.本文我们就介绍如何使用 Golang 提供的 channel(通道) 消除竞争条件. C ...

  10. django--通过jwt获取用户信息的两种方式

    HTTP请求是无状态的,我们通常会使用cookie或session对其进行状态保持,cookie存储在客户端,容易被用户误删,安全性不高,session存储在服务端,在服务器集群情况下需要解决sess ...