如果我已经有一个头,那么模板(-t)文件属于哪里

Where does a template(.t) file belong if i already have a header?

本文关键字:文件 属于 有一个 如果      更新时间:2023-10-16

我应该能够在不更改任何内容的情况下编译这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中的原因。其他代码根本不需要为此而烦恼。