【发布时间】:2012-02-26 19:40:04
【问题描述】:
可能重复:
Why can templates only be implemented in the header file?
Why should the implementation and the declaration of a template class be in the same header file?
我是某所大学的计算机科学专业的学生,我们得到了要为 hw 处理的文件。 而且我不确定这种实例化是如何工作的。
长代码短它看起来像这样。
在 List.h 中
#ifndef _LIST_H_
#define _LIST_H_
#include <iterator>
#include <ostream>
template <class T>
class List
/* implementation below but not relevant to this post */
.
.
.
.
....下面文件的最后几行。
#include "list.cpp"
#include "list_given.cpp"
#endif
并且 List.cpp 不包含 List.h
我不明白在头文件中包含 List.cpp 是如何工作的。
【问题讨论】:
-
不,这不是“显式模板实例化”。
-
如果您将文件命名为“List.*”,还应包括“List.*”(首字母大写),而不是“list.*”。这样您我们就可以不仅在 Windows 上构建您的项目。
-
Cpp 看起来很奇怪,我会在这里使用 hpp
标签: c++ templates header-files