如果我已经有一个头,那么模板(-t)文件属于哪里
Where does a template(.t) file belong if i already have a header?
我应该能够在不更改任何内容的情况下编译这4个文件。我最初以为-t文件是一个头文件,但后来它变成了(.h(文件,我已经有了"arrayi.h"了?有人能帮我正确格式化吗?求你了,谢谢你。
我有这4个文件放在一个项目中:.h/.t/.cpp/client driver.cpp.
低于
// File name : arrayi.t
#ifndef ARRAYI_T_
#define ARRAYI_T_
#include <iostream>
#include <cstdlib>
#include <cassert>
using namespace::std;
// Default constructor for class Array
template<typename T>
Array<T>::Array(int arraySize)
{
cout << "calling the constructor n";
}
// Overloaded output operator for class Array
template<typename T>
ostream &operator<<(ostream &output, const Array<T> &a)
{
int i;
output << "{ ";
for (i = 0; i < a.size; i++)
{
output << a.ptr[i] << ' ';
if ((i + 1) % 10 == 0)
output << "}" << endl;
} //end for
if (i % 10 != 0)
output << "}" << endl;
return output; // enables cout << x << y;
}
#endif
低于.h
// File name : arrayi.h
#ifndef ARRAYI_H_
#define ARRAYI_H_
#include <iostream>
#include <cstdlib>
#include <cassert>
using namespace::std;
template<typename T> class Array;
template<typename T>
ostream &operator<< (ostream& output, const Array<T> &a);
template<typename T>
class Array
{
friend ostream &operator<< <>(ostream &output, const Array<T> &a);
public:
Array(int = 10); //constructor
private:
};
#include "arrayi.t"
#endif
低于的.cpp
// File name : arrayi.cpp
#include "arrayi.h"
#include "arrayi.t"
下方的客户端驱动程序.cpp
// File name : client_driver.cpp
#include<iostream>
using namespace::std;
#include "arrayi.h"
您只需要在.cpp
文件中包含.h
文件。然后编译.cpp
文件,但不编译.h
和.t
文件作为翻译单元。
.t
基本上只是将模板实现分离到另一个文件中。它确实属于头文件,这就是它被包含在.h
中的原因。其他代码根本不需要为此而烦恼。
相关文章:
- .cpp和.h文件中的模板专用化声明
- 为什么两个不同的未命名名称空间可以共存于一个cpp文件中
- 文本文件中的单词链表
- CMake-按正确顺序将项目与C运行时对象文件链接
- 使用新行和不使用新行读取文件
- 在C++程序中输入的文本文件将不起作用,除非文本被复制和粘贴
- 挂起和取消挂起一个文件DLL
- 如何确定我已使用非编码文件到达 EOF?
- 命名空间中具有.h和.cpp文件的类
- 如何使用ndk-build.cmd构建Android.so文件
- 从包含m行的文件中提取n行,必要时(惰性地)重复该文件
- 读取文件并输入到矢量中
- 在C++中查找文件
- c++库的公共头文件中应该包含什么
- 用c++从输入文件中读取另一行
- 如果我已经有一个头,那么模板(-t)文件属于哪里
- 模板专业化是否属于头文件或源文件
- 函数,这些函数接收属于其他文件中类的对象
- 属于Boost文件系统的file_size()的C++错误
- 当向第三方提供库时,C++include文件是否属于安全漏洞