I Have A Few Questions About Setting Up Templates.

Hi guys. I was given a project on Friday that involves using templates in a header file to allow elements in an array to be either integers, floats or doubles. I then have to add a subtraction operator to the class. Ultimately I have to create 3 arrays in my main and assign the difference of the first 2 to the third using templates. For me that's no problem.

The problem I'm having is that my professor never explicitly described how to use templates. He made references to them in class, but he never gave us notes on them. When I asked about the lab all he said was "keep trying" and refused to actually help any of us. So I read the 2 chapters in my textbook on templates, and my book from last semester on templates and I still don't comprehend how to set them up. I understand the idea behind them, just not how to use them correctly.

I have to have 4 files in my project and I am given 2 to begin with.

The first one is the class definition called "arrayi.cpp"

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
// ARRAYi.CPP
// Member function definitions for class Array

// Initialize static data member at file scope
int Array::arrayCount = 0;   // no objects yet

// Default constructor for class Array
Array::Array(int arraySize)
{
  ++arrayCount;             // count one more object
  size = arraySize;         // default size is 10
  ptr = new int[size];      // create space for array
  assert(ptr != 0);  // terminate if memory not allocated
  int i;
  for (i = 0; i < size; i++)
    ptr[i] = 0;            // initialize array
}

// Copy constructor for class Array
Array::Array(const Array &init)
{
  ++arrayCount;             // count one more object
  size = init.size;         // size this object
  ptr = new int[size];      // create space for array
  assert(ptr != 0);  // terminate if memory not allocated
  cout << "copy constr arrayi " << endl;
  int i;
  for (i = 0; i < size; i++)
    ptr[i] = init.ptr[i];  // copy init into object
  cout << endl << "copy constructor working" << endl;
}

// Destructor for class Array
Array::~Array()
{
  --arrayCount;             // one fewer objects
  delete [] ptr;            // reclaim space for array
}

// Get the size of the array
int Array::getSize() const
  {
    return size;
  }

// Overloaded assignment operator
Array &Array::operator=(const Array &right)
{
  if (&right != this)
    {    // check for self-assignment
      delete [] ptr;        // reclaim space
      size = right.size;    // resize this object
      ptr = new int[size];  // create space for array copy
      assert(ptr != 0);     // terminate if memory not allocated
      int i;
      for (i = 0; i < size; i++)
        ptr[i] = right.ptr[i];  // copy array into object
    }
  //this points to ptr to int,  *this returns ptr value
  //ie, the address of the array
  return *this;   // enables x = y = z;
}
// + operator for arrays
Array  Array::operator + (const Array& right)
{
  int large, small;
  if (size > right.size)
    {
      large = size;
      small = right.size;
    }
  else
    {
      large = right.size;
      small = size;
    }
  Array z(large);
  int i;
  for (i = 0; i < small; i++)
    z.ptr[i] =   ptr[i] + right.ptr[i];
  for (i = small; i < large; i++)
    {
      if (right.size == small)
        z.ptr[i] =   ptr[i] ;
      else  z.ptr[i] =   right.ptr[i] ;
    }
  cout << "# of arrays instantiated " << getArrayCount() << endl;
  return z;
}

// Determine if two arrays are equal and
// return 1 if true, 0 if false.
int Array::operator==(const Array &right) const
  {
    if (size != right.size)
      return 0;    // arrays of different sizes
    int i;
    for (i = 0; i < size; i++)
      if (ptr[i] != right.ptr[i])
        return 0; // arrays are not equal

    return 1;       // arrays are equal
  }

// Determine if two arrays are not equal and
// return 1 if true, 0 if false.
int Array::operator!=(const Array &right) const
  {
    if (size != right.size)
      return 1;         // arrays of different sizes
    int i;
    for (i = 0; i < size; i++)
      if (ptr[i] != right.ptr[i])
        return 1;      // arrays are not equal

    return 0;            // arrays are equal
  }

// Overloaded subscript operator
int &Array::operator[](int subscript)
{
  // check for subscript out of range error
  assert(0 <= subscript && subscript < size);
  return ptr[subscript];   // reference return creates lvalue
}

// Return the number of Array objects instantiated
int Array::getArrayCount()
{
  return arrayCount;
}

// Overloaded input operator for class Array;
// inputs values for entire array.
istream &operator>>(istream &input, Array &a)
{
  int i;
  for (i = 0; i < a.size; i++)
    input >> a.ptr[i];

  return input;   // enables cin >> x >> y;
}

// Overloaded output operator for class Array
ostream &operator<<(ostream &output, const Array &a)
{
  int i;
  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;
}






The other file given is the implantation of the first file. This one is called "arrayi.h"

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
//cs132 demo/lab streller
//arrayi.h
//simple array class

#ifndef ARRAYI_H_
#define ARRAYI_H_

#include <iostream>
#include <cstdlib>
#include <cassert>

using namespace std;


class Array
  {
    friend ostream &operator<< <> (ostream &output, const Array<T> &a);
    friend istream &operator>> <>(istream &input, Array<T> &a);

  public:
    Array(int = 10);	//constructor
    Array(const Array &);	//copy constructor
    ~Array();		//destructor
    int getSize() const;	//return size
    Array &operator = (const Array &) ;
    int operator==(const Array &) const;
    int operator != (const Array &) const;
    int &operator[] (int);
    Array operator + (const Array&);
    static int getArrayCount();	//get count of existing
    //array objects 

  private:
    int *ptr;	//ptr to first array element
    int size;	//size of the array
    static int arrayCount;	// #of arrays instantiated
  };


#endif 


I was also told to insert 3 templates into my header file:

1
2
3
4
5
template<typename T> class Array
template<typename T>
ostream &operator << (ostream& output, const Array<T> &a);
template<typename T>
istream &operator >> (istream& input, Array<T> &a);


When I asked what they where for I was told that "they convince the compiler that the 'friends' are templates" and they "need to be inserted into the .h file." I think I know what this means, but I'm not too sure.

So you might be thinking at this point "this guy just wants us to do his homework." Well, I want follow the rules here and I don't want to be lazy, I'm really stuck.

So far I created the project, added in the 2 files given, I created the driver .cpp file that includes the main and a ".t" file for the templates.

My .cpp that has the main in it is this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
//========================================================
// Name: (edited out)
// Class: CS132
// Program name: lab2_client_driver.cpp
// Date created: 9/12/2011
// Date last modified:
// Other files included: arrayi.h, arrayi.t, arrayi.cpp
//========================================================


#include <iostream>
#include "arrayi.h"

 
using namespace std;



int main()
{
	

return 0;
}



And my .t (template) file is this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
//======================================================
// Name: (edited out)
// Template Name: arrayi.t
// Purpose: provide a template to lab2_client_driver.cpp
//          so that elements of the arrays can be either
//          floats, ints or doubles.
// Date Created: 9/12/2011
// Date last modified:
//======================================================


#ifndef ARRAYI_T_
#define ARRAYI_T_

#include <iostream>




#endif 



I follow the link structure I was given at the beginning of the semester. It goes like this:

"driver with main has #include arrayi.h in it," "arrayi.h has #include arrayi.t at the bottom" and "arrayi.cpp has #include arrayi.h in it."

I have all these files in the same project so I know they see each other and there are no errors that say anything to the extend of "file missing" or "invalid file location."

Most of my errors right now are in the "arrayi.cpp" file. They say things like "member function not declared in Array" and other stuff like that. Now I know I have to create templates for every element in the class Array, and I've been tying different templates in the .t file and seeing what works and what doesn't and so far nothing works. I follow examples in my text book and examples I find online of how a template should be set up and I get nothing but errors.

Here are some things I have tried from the text book for the constructor of class Array:(I did modify it in an attempt to get it to work with my project)
1
2
3
4
5
6
template <typename T>
class Array
{
Array(int)

}



Other than that I really have no clue where else to go.

All help is greatly appreciated

Thanks!
Topic archived. No new replies allowed.