经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 程序设计 » C++ » 查看文章
c++模板类的使用,编译的问题
来源:cnblogs  作者:xutopia  时间:2021/12/24 9:09:25  对本文有异议

1,模板类编译的问题

前两天在写代码时,把模板类的声明和分开放在两个文件中了,类似于下面这样:

stack.hpp:

  1. #ifndef _STACK_HPP
  2. #define _STACK_HPP
  3. template <typename Type>
  4. class stack {
  5. public:
  6. stack();
  7. ~stack();
  8. };
  9. #endif

stack.cpp:

  1. #include <iostream>
  2. #include "stack.hpp"
  3. template <typename Type> stack<Type>::stack() {
  4. std::cerr << "Hello, stack " << this << "!" << std::endl;
  5. }
  6. template <typename Type> stack<Type>::~stack() {
  7. std::cerr << "Goodbye, stack " << this << "." << std::endl;
  8. }

main.cpp

  1. #include "stack.hpp"
  2. int main() {
  3. stack<int> s;
  4. return 0;
  5. }

编译

  1. $ g++ -c -o main.o main.cpp
  2. $ g++ -c -o stack.o stack.cpp
  3. $ g++ -o main main.o stack.o
  4. main.o: In function `main':
  5. main.cpp:(.text+0xe): undefined reference to 'stack<int>::stack()'
  6. main.cpp:(.text+0x1c): undefined reference to 'stack<int>::~stack()'
  7. collect2: ld returned 1 exit status
  8. make: *** [program] Error 1

提示找不到函数的定义

在网上寻找的答案如下:

It is not possible to write the implementation of a template class in a separate cpp file and compile. All the ways to do so, if anyone claims, are workarounds to mimic the usage of separate cpp file but practically if you intend to write a template class library and distribute it with header and lib files to hide the implementation, it is simply not possible.

To know why, let us look at the compilation process. The header files are never compiled. They are only preprocessed. The preprocessed code is then clubbed with the cpp file which is actually compiled. Now if the compiler has to generate the appropriate memory layout for the object it needs to know the data type of the template class.

Actually it must be understood that template class is not a class at all but a template for a class the declaration and definition of which is generated by the compiler at compile time after getting the information of the data type from the argument. As long as the memory layout cannot be created, the instructions for the method definition cannot be generated. Remember the first argument of the class method is the 'this' operator. All class methods are converted into individual methods with name mangling and the first parameter as the object which it operates on. The 'this' argument is which actually tells about size of the object which incase of template class is unavailable for the compiler unless the user instantiates the object with a valid type argument. In this case if you put the method definitions in a separate cpp file and try to compile it the object file itself will not be generated with the class information. The compilation will not fail, it would generate the object file but it won't generate any code for the template class in the object file. This is the reason why the linker is unable to find the symbols in the object files and the build fails.

Now what is the alternative to hide important implementation details? As we all know the main objective behind separating interface from implementation is hiding implementation details in binary form. This is where you must separate the data structures and algorithms. Your template classes must represent only data structures not the algorithms. This enables you to hide more valuable implementation details in separate non-templatized class libraries, the classes inside which would work on the template classes or just use them to hold data. The template class would actually contain less code to assign, get and set data. Rest of the work would be done by the algorithm classes.

具体原因就是:

模板类其实就不是一个类,c++的编译器在编译.cpp产生二进制目标文件的时候,需要根据函数的参数类型来确定链接符号(编译器不编译.h文件),而编译模板类的时候因为函数的参数类型都没有确定,所以也就不能产生链接符号,所以在编译阶段是不会报错的,但是在链接阶段就报错了。针对这种情况有三种解决办法,但是最优的还是把实现和声明都放在头文件中。如果不想让c++类显得臃肿,可以在类里面声明,在类外进行实现。

参考链接

https://stackoverflow.com/questions/1724036/splitting-templated-c-classes-into-hpp-cpp-files-is-it-possible

https://www.zhihu.com/question/20630104

https://www.bbsmax.com/A/rV573nLE5P/

https://blog.csdn.net/bangdingshouji/article/details/72832869?utm_medium=distribute.pc_relevant.none-task-blog-OPENSEARCH-3.control&depth_1-utm_source=distribute.pc_relevant.none-task-blog-OPENSEARCH-3.control

原文链接:http://www.cnblogs.com/xutopia/p/15715756.html

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号