This is for school.. but I'm not trying to get you guys to do my homework, I've worked pretty hard on this. This is my sixth term of C++ and I'm having trouble making this 2D array work.
It has a problem in main when I try to set [3][5] to to 'k' but I think this has to do with something in the constructor of Array2D.
Any help would be greatly appreciated.. Even if you could direct me to a webpage that teaches this sort of thing or just tell me why it isn't working. Feel free to change any code that you like. =).
Code:
#include "Array2D.h"
#include <iostream>
using std::endl;
using std::cout;
int main()
{
Array2D<char> ok(5, 10);
Array2D<char> yeah(3, 8);
yeah = ok;
try
{
#error this has a problem too
ok[3][5] = 'k';
}
catch( Exception& e)
{
cout << e.getMessage() << endl;
}
return 0;
}
Code:
#ifndef ARRAY2D_H
#define ARRAY2D_H
#include "Array.h"
#include "Row.h"
#include "Exception.h"
template <typename T>
class Array2D
{
public:
Array2D();
Array2D(int row, int col = 0);
Array2D(const Array2D<T> ©);
~Array2D();
Array2D & operator =(const Array2D<T> & rhs);
Row<T> operator [] (int index);
int getRow();
void setRow( int row );
int getColumn();
void setColumn( int column );
T & Select( int row, int column );
private:
SingleArray<T> m_array;
int m_row;
int m_column;
};
//Default constructor
template <typename T>
Array2D<T>::Array2D() : m_array(0), m_row(0), m_column(0)
{
}
//Two arg constructor sets number of rows and columns
template <typename T>
Array2D<T>::Array2D(int row, int col = 0) : m_row(row), m_column(col)
{
#error idk how this is supposed to work
m_array.setLength( (m_row * m_column) );
}
//Copy Constructor
template <typename T>
Array2D<T>::Array2D(const Array2D ©)
{
*this = copy;
}
//Destructor
template <typename T>
Array2D<T>::~Array2D()
{
m_row = 0;
m_column = 0;
m_array = 0;
}
//Assignment Operator
template <typename T>
Array2D<T> & Array2D<T>::operator =(const Array2D & rhs)
{
if(this != &rhs)
{
m_array.~SingleArray();
m_array = rhs.m_array;
m_row = rhs.m_row;
m_column = rhs.m_column;
}
return *this;
}
template <typename T>
Row<T> Array2D<T>::operator [] (int index)
{
if(index > m_row)
throw Exception("Index exceeds amount of rows");
return Row<T>(*this, index);
}
//Returns the amount of rows
template <typename T>
int Array2D<T>::getRow()
{
return m_row;
}
template <typename T>
void Array2D<T>::setRow( int row )
{
m_row = row;
}
//Returns the amount of columns
template <typename T>
int Array2D<T>::getColumn()
{
return m_column;
}
template <typename T>
void Array2D<T>::setColumn( int column )
{
m_column = column;
}
template <typename T>
T & Array2D<T>::Select( int row, int column )
{
return m_array[m_column * row + column];
}
#endif
Code:
#ifndef SINGLEARRAY_H
#define SINGLEARRAY_H
#include "Exception.h"
/************************************************************************
* Class: Array
*
* Purpose: This class creates a dynamic one-dimensional array that can be * started at any base.
*
* Manager functions:
* SingleArray ( )
* The default size is zero and the base is zero.
* SingleArray (int length, int start_index = 0)
* Creates an appropriate sized array with the starting index
* either zero or the supplied starting value.
* SingleArray (const SingleArray & copy)
* Copy constructor which makes a copy of the array
* operator = (const SingleArray & copy)
* Assignment operator which will assign another type of SingleArray
* the properties of the first SingleArray
* ~SingleArray()
* Deconstructor
*
* Methods:
* operator [ ] (int index)
* returns the index of the array passed in.
* int getStartIndex()
* returns the start index
* void setStartIndex( int start_index)
* Sets the start index to the passed in parameter
* int getLength()
* Returns the length of the array
* void SetLength( int length)
* Sets the length to the passed in parameter
*************************************************************************/
template <typename T>
class SingleArray
{
public:
SingleArray();
SingleArray(int length, int start_index = 0);
SingleArray(const SingleArray<T> ©);
~SingleArray();
SingleArray<T>& operator = (const SingleArray<T> &rhs);
T& operator [] (int index);
int getStartIndex();
void setStartIndex( int start_index );
int getLength();
void setLength( int length );
private:
T * m_array;
int m_length;
int m_start_index;
};
//Default constructor
template <typename T>
SingleArray<T>::SingleArray() : m_array(0), m_length(0), m_start_index(0)
{
}
//Two parameter constructor for setting length and the starting index
template <typename T>
SingleArray<T>::SingleArray(int length, int start_index = 0)
{
m_length = length;
m_start_index = start_index;
m_array = new T[length];
}
//Copy Constructor
template <typename T>
SingleArray<T>::SingleArray(const SingleArray<T> ©)
{
*this = copy;
}
//Destructor
template <typename T>
SingleArray<T>::~SingleArray()
{
delete [] m_array;
m_array = nullptr;
m_length = 0;
m_start_index = 0;
}
//Assignment Operator
template <typename T>
SingleArray<T> & SingleArray<T>::operator = (const SingleArray<T> &rhs)
{
if(this != &rhs)
{
delete [] m_array;
m_array = new T[rhs.m_length];
for(int i = 0; i < m_length; ++i)
{
m_array[i] = rhs.m_array[i];
}
m_length = rhs.m_length;
m_start_index = rhs.m_start_index;
}
return *this;
}
//Bracket operator
template <typename T>
T & SingleArray<T>:: operator [] (int index)
{
index -= m_start_index;
if(index < 0)
throw Exception("Index lower than minimum");
else if(index > m_length)
throw Exception("Index higher than maximum");
return m_array[index];
}
//Returns the start index
template <typename T>
int SingleArray<T>::getStartIndex()
{
return m_start_index;
}
//Sets the start index to the passed in parameter
template <typename T>
void SingleArray<T>::setStartIndex( int start_index )
{
m_start_index = start_index;
}
//Returns the length of the array
template <typename T>
int SingleArray<T>::getLength()
{
return m_length;
}
//Sets the length of the array to the passed in parameter
template <typename T>
void SingleArray<T>::setLength( int length )
{
m_length = length;
}
#endif
Code:
#ifndef ROW_H
#define ROW_H
template <typename T>
class Array2D;
template <typename T>
class Row
{
public:
Row(Array2D<T> & Array, int row);
T & operator [] ( int column);
private:
Array2D<T> & m_array2D;
int m_row;
};
#endif
template <typename T>
Row<T>::Row(Array2D<T> & Array, int row) : m_array2D(Array), m_row(row)
{
}
template <typename T>
T & Row<T>::operator[](int column)
{
if( column > m_array2D.getColumn() )
throw Exception("Index exceeds amount of columns");
return m_array2D.Select(m_row, column);
}