As the title says, one part of my assignment is to implement an array class. The professor gave us the header style to start off with, creating the function declarations for us. Using previous examples, I've tried my best to define these functions, but I'm having a lot of trouble understanding just what's going on. It might be a little much to post an entire header file, but I'll try to explain the issues I'm having regardless. Here is my code:
#ifndef ARRAY_H
#define ARRAY_H
#include <iostream>
#include <string>
using namespace std;
template <class T>
class Array {
private:
int size;
T * arr;
public:
int getSize();
Array();
Array(int size);
Array(Array & other);
Array(T[], int n);
~Array();
Array & operator=(Array & rhs);
Array & operator+(Array & rhs); // append
T & operator[](int i); //allow read and write
const T & operator[](int n) const; // readonly
void print(int n = 5);
void print(ostream & out, int n);
operator int *() { return arr; }
friend ostream & operator <<(ostream & out, Array <T>& rhs);
};
template <class T>
int Array<T>::getSize() {
return size;
}
template <class T>
Array<T>::Array() {
arr = new T[];
}
template <class T>
Array<T>::Array(int size) {
arr = new T[size];
}
template <class T>
Array<T>::Array(Array & other) {
size = other.getSize();
arr = new T[size];
copy(arr[0], arr[size + 1], other.arr[0]);
}
template <class T>
Array<T>::Array(T[], int n) {
size = n;
arr = new T[n];
}
template <class T>
Array<T>::~Array() {
if (arr) {
delete arr;
}
}
template <class T>
Array<T>& Array<T>::operator=(Array & rhs) {
if (this != &rhs) {
delete[] arr;
size = rhs.getSize();
arr = new T[size];
copy(arr[0], arr[size+1], rhs.arr[0]);
}
return *this;
}
template <class T>
Array<T>& Array<T>::operator+(Array & rhs) {
Array *tmp;
tmp = new Array(size + rhs.getSize());
return *tmp;
}
template <class T>
T& Array<T>::operator[](int i) {
assert(0 <= i && i < size);
return arr[i];
}
template <class T>
const T& Array<T>::operator[] (int n) const {
assert(0 <= i && i < size);
return arr[i];
}
template <class T>
void Array<T>::print(ostream &out, int n) {
for (size_t i = 0; i < n; i++) {
out << arr[i] << " ";
}
}
template <class T>
void Array<T>::print(int n = 5) {
print(cout, n);
}
template <class T>
ostream & operator << (ostream & out, Array<T> & rhs) {
out << "( " << rhs.getSize() << ")";
return out;
}
#endif
I'd appreciate any and all hints, just to help me figure out what's going on in just this class. I'm sorry if this ends up being a lot to ask for.
Edit: Thank you all for the help again! The linker errors were fixed. I should've specified that print were the members of the class, instead of just being free.
strcpyfunction is tailored to copy strings, not general data. You might want to usestd::memcpy(orstd::copy).memcpyinstead ofstrcpyfor point 1 & 2Array<T>::Tfor theoperator[]functions.strcpy(arr, other.arr);is nowmemcpy(arr, rhs.arr, sizeof(arr));. No more errors from that come up. For theoperator[]functions, do I need to return an array of the template class?