Template <class T>

Hello you guys...I'm not sure why I keep getting these same errors or maybe it's just me...but could someone please look at my program and tell me what I'm doing wrong: \Main.cpp(11) : error C3861: 'printArray': identifier not found
.\Main.cpp(12) : error C3861: 'printArray': identifier not found
.\Main.cpp(13) : error C3861: 'printArray': identifier not found


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
#include <iostream>
#include <iomanip>
using namespace std;
template<class T>

class Array
{
	friend ostream &operator<<(ostream &, const Array &);
	friend istream &operator>>(istream &, Array &);
public:
	Array(int=10);
	Array(const Array&); //copy constructor
	~Array(); //destructor
	int getsize() const; //return size
	const Array &operator=(const Array&); 
	bool operator==(const Array&)const; //equality operator
	bool operator!=(const Array &right)const //inequality operator; returns opposite of == operator
{
	return !(*this == right);
}
	int &operator[](int); //subscript operator
	const int &operator[](int) const;
	void printArray( const T *Array, const int count);
private:
	int size;
	int *ptr;
};
#include "Array.h"
template<class T>
Array<T>::Array(int Arraysize)
{
	size = (Arraysize >0? Arraysize : 10);
	ptr = new int [size];
	for (int i = 0; i < size; i++)
	ptr[i] = 0;
}
template<class T>
Array<T>::Array(const Array &Arraytocopy):size(Arraytocopy.size)
//copy constructor for class Array. Must receive a reference to prevent infinite recursion.
{
	ptr = new int[size];
	for(int i = 0; i < size; i++)
	ptr[i] = Arraytocopy.ptr[i];
}
template<class T>
Array<T>::~Array()
{
	delete[]ptr;
}
template<class T>
int Array<T>::getsize() const
{
	return size;
}
template<class T>
const Array &Array<T>::operator= (const Array &right) //overload assignment operator
{
	if (&right != this)
    { //if Arrays of different sizes, deallocate original left-side Array,then allocate new left-side Array. 
	if (size != right.size)
	{ 
		delete [] ptr;
		size = right.size;
		ptr = new int[size];
	}
	for (int i = 0; i < size; i++)
	ptr[i] = right.ptr[i];
    }
	return *this;
}
template<class T>
bool Array<T>::operator== (const Array &right) const
{
	if (size != right.size)
		return false;
	for (int i = 0; i < size; i++)
	if (ptr[i] != right.ptr[i])
		return false;
	return true;
}
template<class T>
int &Array<T>::operator[] (int subscript) 
{
	if (subscript < 0 || subscript >= size)
    {
	cout<<"\n Error: SubScript "<<subscript<<" out of range."<<endl;
	exit(1);
    }
	return ptr[subscript];
}
template<class T>
const int &Array<T>::operator[] (int subscript) const
{
	if (subscript < 0 || subscript >= size)
    {
	cout <<"\n Error: SubScript "<<subscript<<" out of range."<<endl;
	exit(10);
    }
	return ptr[subscript];
}
template<class T>
istream &operator>> (istream &input, Array &a)
{
	for(int i = 0; i < a.size; i++)
	input >> a.ptr[i];
	return input;
}
template<class T>
ostream &operator<< (ostream &output, const Array &a)
{
	int i;
	for (i = 0; i < a.size; i++)
    {
	output <<setw(12)<< a.ptr[i];
	if ((i + 1) % 4 == 0)
	output << endl;
    }
	if (i%4 != 0)
	output << endl;
	return output;
}
template<class T>
void printArray( const T *Array, const int count)
{
for ( int i = 0; i < count; i++)
    cout<<Array[i]<<"   ";
cout<<endl;
}
#include "Array.h"

int main()
{
const int acount = 5;
const int bcount = 7;
const int ccount = 6;
int a[acount] = { 1,2,3,4,5};
double b[bcount] = { 1.1,2.2,3.3,4.4,5.5};
char c[ccount] = {"HELLO"};
printArray(a, acount);
printArray(b, bcount);
printArray(c, ccount);
return 0;
}
Last edited on
The declaration is incorrect. You need the template declaration at the head of every function, plus your member functions don't have the correct specifications either.

You need:
1
2
3
4
5
6
7
template<class T>
void printArray(const T *Array, int count)
{
    for (int i = 0; i < count; i++)
        cout<<Array[i]<<"   ";
    cout<<endl;
}


and:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
template<class T>
Array<T>::Array(int Arraysize)
{
    size = (Arraysize >0? Arraysize : 10);
    ptr = new int [size];
    for (int i = 0; i < size; i++)
        ptr[i] = 0;
}

template<class T>
Array<T>::Array(const Array &Arraytocopy):size(Arraytocopy.size)
//copy constructor for class Array. Must receive a reference to prevent infinite recursion.
{
    ptr = new int[size];
    for(int i = 0; i < size; i++)
        ptr[i] = Arraytocopy.ptr[i];
}

template<class T>
Array<T>::~Array()
{
    delete[]ptr;
}
and so on.
so you're telling me that in order for the template class to run...I must implement it in above all the functions in my imp file????Wow...That is some special class there...by the way...what else can this template do that I should be aware of???TY
I've made the correction as you said...but...I'm still getting errors...also I corrected the original program at top to your instructions and how my program now looks.......

Array.h(7) : see declaration of 'Array'
Main.cpp(11) : error C3861: 'printArray': identifier not found
Main.cpp(12) : error C3861: 'printArray': identifier not found
Main.cpp(13) : error C3861: 'printArray': identifier not found
Array.h(15) : see declaration of 'Array<T>::operator ='
definition
'const Array &Array<T>::operator =(const Array<T> &)'
existing declarations
'const Array<T> &Array<T>::operator =(const Array<T> &)'
Array.cpp(29) : error C2955: 'Array' : use of class template requires template argument list

Array.cpp(43) : error C2244: 'Array<T>::operator =' : unable to match function definition to an existing declaration
Last edited on
printArray is a member function; you are not calling it that way.
then what would be the best solution for me to call these 3 ints(counts)???
Your template syntax was incorrect and I was correcting it. You don't need to move it to another file.
Thanks people for the help...I was able to correct and run the program...I couldn't have done it without your support...Once again...Thanks
Topic archived. No new replies allowed.