Template Syntax troubles

So, I wrote a class that dynamically creates Arrays.

Now I'm supposed to turn it into a template, so that it deals with not just int arrays, but also char and string arrays.

I understand the general concept of templates, but the actual syntax is eluding me.

I tried finding examples of templates online, but the online tutorials seem to only describe what templates are, which I already understand.

I followed the syntax of the few examples of actual code that I did find, but then when I tried compiling my program I encountered a slew of errors.

I played around for about two hours and cut down a lot of the errors, but now I've hit a wall. Can someone show me what's wrong with my code (there seems to be something seriously wrong with the cin/cout overloaded functions)?

I don't expect any of you to actually correct the slew of errors here, but can you at least show me one example of something I'm doing wrong? I'm sure I'd be able to fix the rest once I understand the basic idea.


Here's my .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
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
163
//prevent multiple inclusions of header file
#ifndef Array_h
#define Array_h

#include<iostream>
#include <cstdlib>
#include <iomanip>
using namespace std;

template <class T>

class Array
{
        friend ostream & operator << (ostream &, const Array &);
        friend istream & operator >> (istream &, Array &);

     public:
        Array(int = DEFAULTSIZE); // default constructor
        Array(const Array&); // copy constructor

        ~Array(); // destructor

        int getsize() const; // return size

        // assignment operator
        const Array & operator= (const Array &);

        //equality operator
        bool operator == (const Array &) const;

        // inequality operator; returns opposite of == operator
        bool operator != (const Array &) const;

        static int getDELIM();

        T/*int*/ & operator[] (int);

        T/*int*/ operator [] (int) const;

    private:
        static const int DELIM = -999; // example of using a delimiter to signal end of input
        static const int DEFAULTSIZE = 10; // default array size

        int size;
        T/*int*/ *arr;
};
#endif

template<class T> Array<T>::Array(int s)
{
        if(s <= 0)
                size = DEFAULTSIZE;
        else
                size = s;
 arr = new T/*int*/[size];

        if(!arr)
        {
                cerr << "error\n";
                exit(EXIT_FAILURE);
        }
}

template<class T>  Array<T>::Array(const Array &copy)
{
        arr = copy.arr;
}

template<class T> Array<T>::~Array()
{
        delete [] arr;
        arr = NULL;
}

template<class T> T & Array::operator[] (int subscript)
{
        if (subscript < 0 || subscript >= size)
        {
                cerr << "Error";
                exit(EXIT_FAILURE);
        }
        return(arr[subscript]);
}

template<class T> T Array::operator[] (int subscript) const
{
        if (subscript < 0 || subscript >= size)
        {
                cerr << "Error";
                exit(EXIT_FAILURE);
        }
        return (arr[subscript]);
}

//  friend
template<class T> istream & operator >> (istream & is, Array & in)
{
        int count = 0;

        for (int i = 0; i < in.size; i--)
        {
                cin >> in.arr[i];
        }
        return (is);
}

// friend
template<class T> ostream & operator << (ostream & os, const Array & out)
{
        for (int i = 0; i < out.size; i++)
        {
              os << out.arr[i];
        }
        return (os);
}

template<class T> bool Array::operator == (const Array& op2) const
{
        bool arrayEqual = true;

        if(size != op2.size)
                arrayEqual = false;
        else
        {
                for (int i = 0; i < op2.size; i++)
                {
                        if(arr[i] != op2.arr[i])
                                arrayEqual = false;

                        if(!arrayEqual) // If arrayEqual is false once, it is no longer necessary to check and continue the loop
                                i = op2.size;
                }
        }
        return (arrayEqual);
}

template<class T> const Array & Array::operator = (const Array& op2)
{
        if (size >= op2.size)
        {
                for (int i = 0; i <= op2.size; i++)
                {
                        arr[i] = op2.arr[i];
                }
                size = op2.size;
        }
        else
        {
                delete [] arr;
                size = op2.size;
                arr = new T/*int*/[size];
        }
}

template<class T> bool operator != (const Array &right) const
        {
                return ! (*this == right); //invokes Array::operator==
        } // end function operator !-

template<class T> static int getDELIM()
        {
                return(DELIM);
        }



And here's my .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
#include <iostream>
using namespace std;

#include "Array.h"

main()
{
        int size = 0;

        cout << "How large do you want your array to be?\n";
        cin >> size;

        Array<int> arr(size);

        cout << "Enter the values of the array\n";


        for (int i = 0; i < size; i++)
        {
                cin >> arr[i];
        }

        cout << "your numbers are: " << arr << endl;

        cout << "arr[1] = " << arr[0] << endl;

        arr[0] += 7;

        cout << "arr[1] + 7 = " << arr[0] << endl;

        arr[0] -= 3;

        cout << "arr[1] - 3 = " << arr[0] << endl;

        cout << "arr = " << arr << endl;

        cin >> arr[4];

        cout << "arr[5] =(what you just entered)= " << arr[4] << endl;

        cout << arr << endl;
}



Thank you so much.




1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>

template < typename T > struct Array ; // declare Array as a template class

// declare the template function
template < typename T > std::ostream& operator<< ( std::ostream& stm, const Array<T>& a ) ;

template < typename T > struct Array
{
    // declare the friend function as a template with <>
    friend std::ostream& operator<< <> ( std::ostream& stm, const Array<T>& a ) ;

    // ...
};

// implement the friend funcytion
template < typename T > std::ostream& operator<< ( std::ostream& stm, const Array<T>& a )
{
    // TODO: print array to stm

    return stm ;
}


See: http://www.parashift.com/c++-faq-lite/template-friends.html
Ahh, thank you very much.

I implemented the changes you showed me, and, after some playing around, most of the compilation errors vanished.

From there, the remaining compilation errors made sense and I corrected them and now have a fully working program!

Thank you very much!
Topic archived. No new replies allowed.