0

I should be able get these 4 files to compile without changing anything. I first thought the .t file was a header file but then it makes it a (.h) file, and i already have "arrayi.h" for that? Can anyone help me format them correctly? Please and thank you.

I have this 4 files to put in 1 project: .h / .t / .cpp / client driver.cpp.

.t below

// 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 below

// 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 below

// File name         : arrayi.cpp


#include "arrayi.h"

#include "arrayi.t"

client driver.cpp below

// File name         : client_driver.cpp


#include<iostream>
using namespace::std;

#include "arrayi.h"
Guillaume Racicot
  • 39,621
  • 9
  • 77
  • 141
  • 1
    `using namespace ::std` in a header file (or in the `.t` file) [is a big no-no](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice). – walnut Feb 19 '20 at 20:41

1 Answers1

0

You only need to include the .h file in your .cpp files. Then compile the .cpp files, but not the .h and .t files, as translation units.

The .t is basically only meant to separate the template implementation into another file. It really belongs into the header file and that is why it is included in the .h. Other code need not bother with it at all.

walnut
  • 21,629
  • 4
  • 23
  • 59