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.
#include <iostream>
#include "array.h"
#include "array_exception.h"
usingnamespace 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) {
returnfalse;
}
returntrue;
}
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
@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?
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!