What we are to do is pretty much mentioned in the comments of the code.
I have to make a class ARRAY, which searches a list etc...
Right now I can't compile this and I'm so stuck. Please help :)
Array.h
// Put comments about the class here.
#ifndef ARRAY_H
#define ARRAY_H
#include <iostream>
using namespace std;
template <class T>
class Array
{
public:
static const int NOT_FOUND = -1;
static const int DEFAULT_SIZE = 100;
Array(int size = DEFAULT_SIZE); // Constructor
// Creates an array of the size specified by the parameter size. The default array size is 100.
// Postcondition: The list points to the array, length = size
// Copy constructor
Array(const Array<T>& rhs);
~Array(); // Destructor
// Deallocates the memory occupied by the array.
// Overloads the assignment operator
const Array<T>& operator= (const Array<T>&);
// Overloads the equality operator
bool operator== (const Array & rhs) const;
// Overloads the non-equality operator
bool operator!= (const Array & rhs) const;
// Method to determine the size of the array.
int get_size (void) const;
const T& operator[](int index) const;// Method to return an element in the array that will not be changed.
T& operator[](int index); // Method to return an element in the array that can be changed.
// Method to search the list for the first instance of the given item.
int search(const T& itemFound) const;
// Postcondition: If the item is found, returns the location in the array where the item is first found; otherwise, returns -1.
int find_first (const T & element) const;
// Function to output the elements of the list to the ostream
// passed in.
template <typename U>
friend ostream & operator<< (ostream & os,
const Array<U> & array);
private:
T * elements_; //pointer to the array elements
int length_; //the length of the list
int position_;
int size_;
};
/* Define the methods in the header file since the compiler
** needs these definitions to create the element-specific array
** class.
*/
// Constructor
template <class T>
Array<T>::Array(int size)
: size_(size), elements_(new T[size])
{
position_ = 0;
}
// Copy constructor
//template <class T>
//Array<T>(const T& copy)
//: size_(copy.size), elements_(new T[copy.size])
//{
// copy(copy.elements_, copy.elements_+ copy.size, elements_); // #include <algorithm> for std::copy
//}
// Destructor
template <class T>
Array<T>::~Array()
{
delete []elements_;
}
// Overloads the assignment operator
template <class T>
const Array<T>&
Array<T>::operator= (const Array<T>& rhs)
{
if( size_ != rhs.size_ )//also checks if on both side same object
{
delete []elements_;
elements_= new int[rhs.size];
}
size_=rhs.size;
position_=rhs.position_;
for( int i = 0; i < position_; i++ )
elements_[i]=rhs.elements_[i];
return *this;
}
// Overloads the equality operator return! (*this == another_object)
// Overloads the non-equality operator
//bool Array::operator!=(const Array &array)
//{
// return !(*this == another_object);
//}
// Method to determine the size of the array.
//const T& Array<T>::sizeOfArray()
// Method to return an element in the array that will not be changed.
template <class T>
const T& Array<T>::operator[](int index)const
{
return this->elements_[index];
}
// Method to return an element in the array that can be changed.
template <class T>
T& Array<T>::operator[](int index)
{
return this->elements_[index];
}
// Method to search the list for the first instance of the given item.
template <class T>
int Array<T>::search(const T& itemFound) const
{
bool itemIsFound = false;
int location;
for(location = 0; location < size_; location++)
{
if(elements_[location] == itemFound)
{
itemIsFound = true;
}
}
if(itemIsFound)
return location;
else
return -1;
}
// Function to output the elements of the list to the ostream passed in.
#endif
test.cpp
#include <assert.h>
#include "Array.h"
using namespace std;
int
main (int argc,
char *argv[])
{
// Create and initialize an array of ints
Array<int> int_array1;
for (int i = 0; i < Array<int>::DEFAULT_SIZE; ++i)
{
int_array1[i] = i;
}
// Right now the code below just prints out the array
// and requires visual checking which is tedious and error-prone.
// Change testing the output of an array via a script.
// CHANGE THIS
std::cout << int_array1 << std::endl;
const Array<int> int_array2 (int_array1);
// Check copy constructor
assert (int_array1 == int_array2);
// Create another int array to check !=
Array<int> int_array3;
for (int i = Array<int>::DEFAULT_SIZE; i > 0; --i)
{
int_array3[i] = i - 1;
}
// Check !=
assert (int_array1 != int_array3);
// Change an element in the first array so it's no longer
// equal to the second array.
int_array1[3] = 0;
assert (int_array1 != int_array2);
// Test the find_first method.
assert (int_array1.find_first (10) == 10);
// Set an earlier element to the value 10 and recheck
// the find_first method.
int_array1[4] = 10;
assert (int_array1.find_first (10) == 4);
// ADD MORE TESTS TO CHECK *ALL* THE METHODS ON THE ARRAY CLASS.
return 0;
}