为什么编译器在.cpp中找不到模板定义 [英] why a compiler can't find a template definition in .cpp
本文介绍了为什么编译器在.cpp中找不到模板定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想知道,为什么一个c ++编译器不能在一个.cpp文件中定义一个模板类?
I'm wondering, why a c++ compiler is not able to instanciate a template class if it is defined in a .cpp file ?
编辑:对不起,但我已经做了一个搜索之前,但回答这个显然是相同的问题是编译器不能找到一个模板定义在.cpp,但不告诉为什么。除非我有坏的阅读答案。
I am sorry but I have done a search before but answer to this apparently same question are "a compiler can't find a template definition in .cpp" but don't tell why. Except if i have bad read answers.
推荐答案
C ++常见问题说明:
The C++ FAQ says it all:
- 模板不是类或函数。模板是编译器用来生成一系列类或函数的模式。
- 为了让编译器生成代码,它必须同时看到模板定义只是声明)和特定类型/用于填写模板的任何内容。例如,如果你想使用Foo,编译器必须同时看到Foo模板和你试图创建一个特定的Foo的事实。
- 你的编译器可能没有在编译另一个.cpp文件时,记住一个.cpp文件的详细信息。它可以,但大多数不,如果你正在阅读这个常见问题,它几乎肯定不是。 BTW这被称为单独的编译模型。
http://www.parashift.com/c++-faq-lite/templates.html#faq-35.12
这篇关于为什么编译器在.cpp中找不到模板定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文