0

MMmmmmk so what i am trying to do is simply throw a the ERROR object from the Array<T>::operator[] and catch it in main take a took... it prints out the equivalent of an empty space in memory.

//error.h
class ERROR
{
public:
	ERROR(char * Incomming)
	{
		m_message = new char[strlen(Incomming)+1];
		strcpy(m_message, Incomming);
	};
	void GetMessage(){std::cerr<<m_message<<std::endl;};
	~ERROR(){delete m_message;};
private:
	char * m_message;
	

};

//array.h
template <typename T>
T &Array<T>::operator[](int const &index)    //throw(ERROR) doesn't work
{
	int newindex = index - m_start_index;

		if(newindex == m_length)
			throw ERROR("Out Of Bounds");

	return m_array[newindex];
}

//main.cpp
for(int i(arrInt2.getStartIndex()); i<arrInt2.getLength()+arrInt2.getStartIndex()+1; i++)
{
	try{std::cout<<arrInt2[i]<<" ";}
	catch(ERROR *er)
	{
	        er->GetMessage();
	}

}

anything you guys see?

2
Contributors
2
Replies
3
Views
9 Years
Discussion Span
Last Post by jobob64
0

don't try to catch a pointer to the object, because you have thrown the ERROR object (not a pointer to the ERROR object).

use catch(ERROR &er){er.GetMessage();} in the main code.

you could also use catch(ERROR er){er.GetMessage();} in the main code.

0

its just a pointer because i was swapping em out, trying const ref, ref, value, pointer etc and it just ended up at pointer when i posted... but i made the ERROR class canonical and it worked thanks!

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.