Compiling Errors in Template Class

closed account (S3TkoG1T)
Hello,

I need help in figuring out what my compiling errors mean, and what I should do to fix them: The following is my header & Implementation files. Note, the purpose of this class is a built in Array.

Header File:
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
#ifndef ARRAY_H
#define ARRAY_H

#include <iostream>
#include <algorithm>

template <typename T>
class Array;

template <typename T>
std::ostream &operator <<(std::ostream &os, const Array<T> &array);

template <typename T>
class Array;

template <typename T>
T operator +(const Array<T> &arr1, const Array<T> &arr2);	

template <typename T>
class Array;

template <typename T>
T operator +(const Array<T> &array, const T &i);


template <typename T>
class Array {		
	friend std::ostream &operator << <>(std::ostream &os, const Array<T> &array);
	friend Array operator + <>(const Array<T> &arr1, const Array<T> &arr2);	
	friend Array operator + <>(const Array<T> &array, const T &i);

public:
	Array(int capacity=100) : arr(new T[capacity]), capacity(capacity), size(0) {}
	Array(const Array &other); //copy constructor
	~Array() {delete [] arr;}
	Array<T> &operator =(const Array<T> &rhs);
	T &operator [](int index);
	const T &operator [](int index) const;
	Array<T> &operator +=(T &val);
	int getSize() {return size;}
	Array<T> &operator +=(const Array<T> &arr);  
	bool isEmpty() {return size == 0;}
	bool contains(int val) const;
	int find(int val) const;

private:
	void checkCapacity();
	T *arr;
	int capacity;
	int size;
};



#endif 


Implementation Code:
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
#include <iostream>

#include "array.h"
#include "array_exception.h"

using namespace std;

#define DEBUG true

template <typename T>
std::ostream &operator <<(std::ostream &os, const Array<T> &array) {
	os << "{";
	for(int i = 0; i < array.size; i++)
		os << array.arr[i] << (i < array.size-1 ? ", " : "");
		os << "}";
		return os;
	}
template <typename T>
Array<T>::Array(const Array &other) : arr(new T[other.capacity]), capacity(other.capacity), size(other.size) {
	for(int i =0; i < size; i++)
		arr[i] = other.arr[i];
} 

template <typename T>
Array<T> &Array::operator =(const Array<T> &rhs) {
	if(this == &rhs) return *this;
	delete [] arr;
	size = rhs.size;
	capacity = rhs.capacity;
	arr = new T[capacity]; //
	for(int i = 0; i < size; i++) 
		arr[i] = rhs.arr[i];
	return *this;
}

template <typename T>
T &Array<T>::operator [](int index) {
	if(index < 0 || index >= size) throw ArrayException("Index out of bounds");
	return arr[index];
}

template <typename T>
const T &Array<T>::operator [](int index) const {
	if(index < 0 || index >= size) throw ArrayException("Index out of bounds");
	return arr[index];
}

template <typename T>
Array<T> &Array::operator +=(T &val) {
	checkCapacity();
	arr[size] = val;
	size++;
	return *this;
}

template <typename T>
void Array::checkCapacity() {
	if (size < capacity) return;

	capacity *= 2;
	int *newArr = new T[capacity];
	for (int i = 0; i < size; i++)
		newArr[i] = arr[i];
	delete [] arr;
	arr = newArr;
	if (DEBUG) cerr << "Increased array capacity to " << capacity << endl;
}

template <typename T>
T operator +(const Array<T> &arr1, const Array<T> &arr2) {
	Array result = arr1;
	return result += arr2;
} // not sure

template <typename T>
Array<T> &Array::operator +=(const Array<T> &arr) {
	size += arr.size;
	return *this;
}

template <typename T>
bool Array::contains(int val) const {
	if(val == size) {
		return false;
	}
	return true;
}

template <typename T>
int Array::find(int val) const {
	for(int i = 0; i < val; i++) {
		if(i == val) {
			return i;
		}
	}
	return -1;
}


Compiling Errors:

array.cpp:25:11: error: ‘template<class T> class Array’ used without template parameters
array.cpp:25:48: error: ‘Array<T>& operator=(const Array<T>&)’ must be a nonstatic member function
array.cpp:49:11: error: ‘template<class T> class Array’ used without template parameters
array.cpp:49:36: error: ‘Array<T>& operator+=(T&)’ must take exactly two arguments
array.cpp:57:6: error: ‘template<class T> class Array’ used without template parameters
array.cpp: In function ‘void checkCapacity()’:
array.cpp:58:6: error: ‘size’ was not declared in this scope
array.cpp:58:13: error: ‘capacity’ was not declared in this scope
array.cpp:60:2: error: ‘capacity’ was not declared in this scope
array.cpp:62:22: error: ‘size’ was not declared in this scope
array.cpp:63:15: error: ‘arr’ was not declared in this scope
array.cpp:64:12: error: ‘arr’ was not declared in this scope
array.cpp: In function ‘T operator+(const Array<T>&, const Array<T>&)’:
array.cpp:71:8: error: missing template arguments before ‘result’
array.cpp:71:8: error: expected ‘;’ before ‘result’
array.cpp:72:9: error: ‘result’ was not declared in this scope
array.cpp: At global scope:
array.cpp:76:11: error: ‘template<class T> class Array’ used without template parameters
array.cpp:76:49: error: ‘Array<T>& operator+=(const Array<T>&)’ must take exactly two arguments
array.cpp:82:6: error: ‘template<class T> class Array’ used without template parameters
array.cpp:82:31: error: non-member function ‘bool contains(int)’ cannot have cv-qualifier
array.cpp: In function ‘bool contains(int)’:
array.cpp:83:12: error: ‘size’ was not declared in this scope
array.cpp: At global scope:
array.cpp:90:5: error: ‘template<class T> class Array’ used without template parameters
array.cpp:90:26: error: non-member function ‘int find(int)’ cannot have cv-qualifier


Thanks for the feedback!
Last edited on
Template classes must either be defined in the header or include the implementation at the bottom of the header.
I would advise you to do not use forward declaration if possible.

Also:
25
26
Array<T> &Array<T>::operator =(const Array<T> &rhs) {
              //↑Forgot this 
Lime 49 — the same
closed account (S3TkoG1T)
@naraku 9333: Why is it that I can't have a separate implementation file? In other words, why do I have to define my template at the bottom of the header?
closed account (S3TkoG1T)
MiiNiPaa: Forward declaration? Can you specify where? :)

Thanks for your input!
closed account (S3TkoG1T)
naraku9333: Thank you so much!! after I included implementation at the bottom of the header I was able to fix every single compiling error! But if possible, can you explain why doing so is important? And why before as a separate cpp file it wouldn't work?

MiiniPaa: You also helped me too!! Thanks a bunch!
Topic archived. No new replies allowed.