在剑指offer的练习10中,需利用两个栈实现队列的效果。在第一个版本中,我完成了int类型的效果实现。

9_Use_Two_Stack_Realization_Queue.h:

#ifndef Use_Two_Stack_Realization_Queue_H
#define Use_Two_Stack_Realization_Queue_H
#include "header.h"class stack_to_queue
{
private:stack<int> stack_base;stack<int> stack_queue;
public:stack_to_queue();~stack_to_queue();void appendTail(int value);void deleteHead();
};

9_Use_Two_Stack_Realization_Queue.cpp:

#include "9_Use_Two_Stack_Realization_Queue.h"stack_to_queue::stack_to_queue()
{
}stack_to_queue::~stack_to_queue()
{
}void stack_to_queue::appendTail(int value)
{stack_base.push(value);cout<<"now the queue have "<<stack_base.size()+stack_queue.size()<<" size"<<endl;
}
void stack_to_queue::deleteHead()
{if (stack_queue.empty()){while (!stack_base.empty()){stack_queue.push(stack_base.top());stack_base.pop();}}if (stack_queue.empty())cout << "there have no message in queue" << endl;else{cout << "delete the head value:" << stack_queue.top() << endl;stack_queue.pop();}cout<<"now the queue have "<<stack_base.size()+stack_queue.size()<<" size"<<endl;
}

这时代码可以正常地被测试:

void test9()
{stack_to_queue st;st.appendTail(1);st.appendTail(2);st.appendTail(3);st.deleteHead();st.deleteHead();st.appendTail(4);st.appendTail(5);st.deleteHead();st.deleteHead();st.deleteHead();st.deleteHead();
}
int main(int argc, char *argv[])
{clock_t start,end;start=clock();test9();end=clock();cout<<endl<<"this function using :"<<double(end-start)/CLOCKS_PER_SEC<<"s"<<endl;return 0;
}
root@cephtest:/home/jzoffer# ./test
now the queue have 1 size
now the queue have 2 size
now the queue have 3 size
delete the head value:1
now the queue have 2 size
delete the head value:2
now the queue have 1 size
now the queue have 2 size
now the queue have 3 size
delete the head value:3
now the queue have 2 size
delete the head value:4
now the queue have 1 size
delete the head value:5
now the queue have 0 size
there have no message in queue
now the queue have 0 sizethis function using :0.000263s

然而目前的代码只能接受int类型。进一步的,使用模板template <typename T>来使代码能够接受不同类型。然而修改后编译程序报错:

root@cephtest:/home/jzoffer# make
Scanning dependencies of target test
[ 11%] Building CXX object CMakeFiles/test.dir/10_Fibonacci_Sequence.cpp.o
[ 22%] Building CXX object CMakeFiles/test.dir/4_FindNFrom2Arry.cpp.o
[ 33%] Building CXX object CMakeFiles/test.dir/5_Change_Space_To_Otherchars.cpp.o
[ 44%] Building CXX object CMakeFiles/test.dir/6_EndToFirst_From_LinkNode.cpp.o
[ 55%] Building CXX object CMakeFiles/test.dir/7_Rebuild_BinaryTree.cpp.o
[ 66%] Building CXX object CMakeFiles/test.dir/8_FindNextNode_From_Tree.cpp.o
[ 77%] Building CXX object CMakeFiles/test.dir/9_Use_Two_Stack_Realization_Queue.cpp.o
[ 88%] Building CXX object CMakeFiles/test.dir/main.cpp.o
[100%] Linking CXX executable test
CMakeFiles/test.dir/main.cpp.o: In function `test9()':
main.cpp:(.text+0x497): undefined reference to `stack_to_queue<int>::stack_to_queue()'
main.cpp:(.text+0x4ab): undefined reference to `stack_to_queue<int>::appendTail(int)'
main.cpp:(.text+0x4bf): undefined reference to `stack_to_queue<int>::appendTail(int)'
main.cpp:(.text+0x4d3): undefined reference to `stack_to_queue<int>::appendTail(int)'
main.cpp:(.text+0x4e2): undefined reference to `stack_to_queue<int>::deleteHead()'
main.cpp:(.text+0x4f1): undefined reference to `stack_to_queue<int>::deleteHead()'
main.cpp:(.text+0x505): undefined reference to `stack_to_queue<int>::appendTail(int)'
main.cpp:(.text+0x519): undefined reference to `stack_to_queue<int>::appendTail(int)'
main.cpp:(.text+0x528): undefined reference to `stack_to_queue<int>::deleteHead()'
main.cpp:(.text+0x537): undefined reference to `stack_to_queue<int>::deleteHead()'
main.cpp:(.text+0x546): undefined reference to `stack_to_queue<int>::deleteHead()'
main.cpp:(.text+0x555): undefined reference to `stack_to_queue<int>::deleteHead()'
main.cpp:(.text+0x564): undefined reference to `stack_to_queue<int>::~stack_to_queue()'
main.cpp:(.text+0x588): undefined reference to `stack_to_queue<int>::~stack_to_queue()'
collect2: error: ld returned 1 exit status
CMakeFiles/test.dir/build.make:276: recipe for target 'test' failed
make[2]: *** [test] Error 1
CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/test.dir/all' failed
make[1]: *** [CMakeFiles/test.dir/all] Error 2
Makefile:83: recipe for target 'all' failed
make: *** [all] Error 2

有三种方法可以解决该问题,分别是:

1、在.cpp文件的最尾,实例化所需要的类型:

template class stack_to_queue<int>;
template class stack_to_queue<char>;

2、将.cpp文件中的代码全部移动到.h文件中。相当于不再见声明和实现分离。

3、将.cpp文件重命名为xxx.h文件,然后在原来的.h文件中包含该xxx.h头文件

以上方法任选一种即可使程序正常运行。

调用模板类出现undefined reference to的情况及解决方案相关推荐

  1. undefined reference 问题各种情况分析

    扒自网友文章 关于undefined reference这样的问题,大家其实经常会遇到,在此,我以详细地示例给出常见错误的各种原因以及解决方法,希望对初学者有所帮助. 1.  链接时缺失了相关目标文件 ...

  2. 编译opencv4.2时出现undefined reference to `jpeg_default_qtables‘错误的解决方案

    操作系统:kylinV10 opencv版本:4.2.0 在编译opencv4.2.0的版本时,出现undefined reference to `jpeg_default_qtables'的错误提示 ...

  3. TermCriteria模板类

    学习写vo过程中遇到的 类功能:模板类,作为迭代算法的终止条件. 构造函数: TermCriteria(int type,int maxCount,double epsilon); 参数说明: typ ...

  4. GCC 链接时出现undefined reference to “...”时可能解决办法

    undefined reference to "..." 顾名思义就是没找到,没找到的原因有两个 1.不存在.不存在就想办法加进去. 2.找错地方. GCC链接时的特点是按顺序连接 ...

  5. c++模板 --- 类模板、自定义类型当做模板参数

    生成一个类模板 类中用到了未知类型叫做类模板 用 template 修饰的类,这个类就是一个模板类 多用在数据结构中,忽略类型的问题 只要被 template 修饰,就是一个模板类,有没有用未知类型都 ...

  6. libboost_filesystem.so: undefined reference to

    caffe 编译时出现的错误: /usr/lib64/libboost_filesystem.so: undefined reference to `boost::system::get_system ...

  7. eclipse里调用接口库时出现了错误 Undefined reference to

    eclipse里调用接口库时出现了错误,从http://space.itpub.net/2008/viewspace-45218找到了原因,记录如下,待解决后再做修改. ############### ...

  8. 2021-03-10 模板扩展类调用模板基类成员函数

    侯捷书内 : 扩展模板类 内通过  this->  指模板基类的成员函数,或通过   基类名::  的方式调用 基类成员函数 侯捷书内说: 扩展模板类,找不到模板基类的成员函数 但是在vs201 ...

  9. C++调用C的函数,出现 undefined reference to 的解决办法

    假设我写了一个aaaa.h , aaaa.c  来实现一组函数功能 #ifndef AAAA_H #define AAAA_H#include<stdio.h>void fun();#en ...

最新文章

  1. 眉目传情之匠心独运的kfifo【转】
  2. Magento多图导入
  3. 类与接口(五)java多态、方法重写、隐藏
  4. php curl 防止采集,php多线程采集网页的解决办法 curl多线程采集
  5. 关于引入 js 文件
  6. 用Java语言编写的特殊算法
  7. SpringBoot使用ResponseBodyAdvice进行统一响应处理
  8. 最简单的php导出excel文件方法
  9. 【NIPS 2017】基于深度强化学习的想象力增强智能体
  10. 【kindeditor】kindeditor的使用
  11. Web API 处理机制剖析 --- 拨开迷雾看本质
  12. 人生苦短我要学python壁纸_人生苦短我学Python——环境安装
  13. 为了战略发展,Adobe推出自己浏览器?
  14. Bluno 是干什么的?- 云物联戒烟设备的可选原型之一
  15. python小波图像去噪_小波去噪
  16. 小米论坛php,黑橙新版小米社区discuz模板
  17. Manjaro-KDE安装动态桌面插件
  18. Android使用AudioManager切换到听筒模式
  19. Oracle中的sql语句
  20. 【ArcGIS教程】(1)带有经纬度的EXCEL数据如何转换为shp矢量数据?

热门文章

  1. 11个相似图片搜索网站(以图找图)[转]
  2. MySQL 命令环境变量设置方法
  3. 猴子吃桃,C语言,递归法
  4. 评估回归模型的指标:MSE、RMSE、MAE、R2、偏差和方差
  5. c语言实现顺序存储程序,线性表的顺序存储结构动态态分配C语言实现
  6. LTS = Long Term Support:长期支持版本
  7. linux(4)进程控制
  8. 网络云存储技术Windows server 2012 (项目九 存储服务器文件的安全性配置与管理)
  9. 面渣逆袭:三万字,七十图,详解计算机网络六十二问(收藏版)
  10. ❤️连续面试失败后,我总结了57道面试真题❤️,如果时光可以倒流...(附答案,建议收藏)