Can't throw exception in Template
Feb 24, 2013 at 9:59pm UTC
I'm making an "improved" array for my programing class. It's currently unfinished, so you might see some commented out code. I'm trying to debug what I have.
I keep getting these errors when I try to complile my main.cpp:
In file included from main.cpp:3:0:
array.h:107:43: error: expected type-specifier before ‘out_of_range’
array.h:107:43: error: expected ‘)’ before ‘out_of_range’
array.h:107:43: error: expected initializer before ‘out_of_range’
array.h:121:55: error: expected type-specifier before ‘out_of_range’
array.h:121:55: error: expected ‘)’ before ‘out_of_range’
array.h:121:55: error: expected initializer before ‘out_of_range’
My main file:
1 2 3 4 5 6 7 8 9 10 11 12
#include <iostream>
#include <stdexcept>
#include "array.h"
using namespace std;
using namespace ArrayNameSpace;
int main()
{
Array<int > testSubject(5);
return 0;
}//End main
aannnd my Array.h 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 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 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
//ADD CONSTS TO METHODS
//CURRENTLY WORKING ON EXCEPTIONS AND BRACKET[] OVERLOADS. I HAVE TO FIGURE OUT SOLUTIONS FOR REACHING
//INDEXES FOR CHARS AND ENUMS
#ifndef __array_H__
#define __array_H__
#include <stdexcept>
namespace ArrayNameSpace
{
/************************CLASS DECLERATION****************************
* *******************************************************************/
template <class T>
class Array
{
public :
explicit Array();
explicit Array(int );
explicit Array(int , int );
explicit Array(char , char );
~Array(); //destructor
Array(const Array&); //copy constructor
// explicit Array(enum enumObject);
T& operator [](int index);
const T& operator [](int index) const ;
const Array& operator =(const Array& other);
private :
int arraySize;
int count;
int startIndiceInt;
int endIndiceInt;
int fillCount;
char startIndiceChar;
char endIndiceChar;
T* theArray;
};//End class defenition
/************************CLASS IMPLEMENTATION*************************
***************************CONSTRUCTORS******************************/
template <class T>
Array<T>::Array()
{
arraySize = 0;
theArray = new T[arraySize];
fillCount = 0;
}//End constructor
template <class T>
Array<T>::Array(int n)
{
arraySize = n;
theArray= new T[arraySize];
fillCount = 0;
}//End constructor
template <class T>
Array<T>::Array(int m, int n)
{
arraySize = n-m+1;
startIndiceInt = m;
endIndiceInt = n;
theArray = new T[arraySize];
fillCount = 0;
}//End constructor
template <class T>
Array<T>::Array(char m, char n)
{
arraySize = n-m+1;
startIndiceChar = m;
endIndiceChar = n;
theArray = new T[arraySize];
fillCount = 0;
}//End constructor
/************************CLASS IMPLEMENTATION*************************
****************************DESTRUCTORS******************************/
template <class T>
Array<T>::~Array()
{
delete [] theArray;
theArray = NULL;
}//End destructor
/************************CLASS IMPLEMENTATION*************************
*************************COPY CONSTRUCTORS***************************/
template <class T>
Array<T>::Array(const Array<T>& anArray)
{
arraySize = anArray.arraySize;
theArray = new T[arraySize];
count = anArray.count;
for (int i = 0; i++; i< count)
{
theArray[i] = anArray.theArray[i];
}//End for loop
}//End copy constructor
/************************CLASS IMPLEMENTATION*************************
*************************OPERATOR OVERLOADS**************************/
template <class T>
T& Array<T>::operator [](int index) throw (out_of_range)
{
// throw an exception if outside the array boundaries
if (index >= arraySize || index<= 0)
{
throw out_of_range( "The given index (" + index + ")" + " does not exist within the Array's boundaries." );
}
else
return theArray[index];
}//End operator overload []
// the following method allows a const pass by reference argument
// For example: void func(const Array& array)
template <class T>
const T& Array<T>::operator [](int index) const throw (out_of_range)
{
// throw an exception if outside the array boundaries
if (index >= arraySize || index<= 0)
{
throw out_of_range( index + " does not exist within the Array's boundaries." );
}
else
return theArray[index];
}//End operator overload [] const Pass by reference
template <class T>
const Array<T>& Array<T>::operator =(const Array<T>& other)
{
if ( &other == this )
return *this ;
delete [] theArray;
count = other.count;
arraySize = other.arraySize;
theArray = new T[arraySize];
for (int i = 0; i < count; i++)
{
theArray[i] = other.theArray[i];
}//End for loop
return *this ;
}//End operator overload =
}//End ArrayNameSpace
#endif
I've spent at least a good two hours trying to google solutions
Feb 24, 2013 at 10:03pm UTC
std:: out_of_range
Feb 24, 2013 at 10:03pm UTC
T& Array<T>::operator [](int index) throw (std:: out_of_range)
You forgot the namespace.
Last edited on Feb 24, 2013 at 10:04pm UTC
Feb 24, 2013 at 10:08pm UTC
Thank you for the prompt response, I appreciate it!
Topic archived. No new replies allowed.