ispcorp
asked on
Error "throws different exception than previous declaration"
Hello,
I'm getting this error in several different places, but here is a copy of one of the error lines...
C:\Project1\studentlist.cp p declaration of `void StudentList::remove(int)' throws different exceptions C:\Project1\studentlist.h than previous declaration `void StudentList::remove(int) throw (ListIndexOutOfRangeExcept ion)'
What does this mean? Thanks, and if you need me to post up some code, please don't hesitate to ask....
I'm getting this error in several different places, but here is a copy of one of the error lines...
C:\Project1\studentlist.cp
What does this mean? Thanks, and if you need me to post up some code, please don't hesitate to ask....
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
IMPLEMENTATION CODE:
#include "studentlist.h" // class's header file
#include <cstddef>
#include <cassert>
StudentList::StudentList() : size(0),head(NULL)
{}
StudentList::~StudentList( )
{}
bool StudentList::isEmpty() const
{
return size == 0;
}
int StudentList::getLength() const
{
return size;
}
StudentList::Node *StudentList::find(int index) const
{
if ((index < 1) || (index > getLength()))
return NULL;
else
{
Node *cur = head;
for (int skip = 1; skip < index; ++skip)
cur = cur->next;
return cur;
}
}
void StudentList::retreive(int index,ItemType& dataItem) const
{
if ((index < 1) || (index > getLength()))
throw ListIndexOutOfRangeExcepti on("ListOu tOfRangeEx ception: retrieve index out of range");
else
{
Node *cur = find(index);
dataItem = cur->item;
}
}
void StudentList::insert(int index,ItemType newItem)
{
int newLength = getLength() + 1;
if ((index < 1) || (index > newLength))
throw ListIndexOutOfRangeExcepti on("ListOu tRangeExce ption: insert index out of range");
else
{
Node *newPtr = new Node;
if (newPtr == NULL)
throw ListException("ListExcepti on: insert cannot allocate memory");
else if(size==MAX)
throw ListException("ListExcepti on: maximum number of students have been reached");
else
{
size = newLength;
newPtr->item=newItem;
if (index==1)
{
newPtr->next=head;
head=newPtr;
}
else
{
Node *prev = find(index-1);
newPtr->next=prev->next;
prev->next=newPtr;
}
}
}
}
void StudentList::remove(int index)
{
Node *cur;
if ((index < 1) || (index > getLength()))
throw ListIndexOutOfRangeExcepti on("ListOu tRangeExce ption: remove index out of range");
else
{
if (index ==1)
{
cur = head;
head = head->next;
}
else
{
Node *prev = find(index-1);
cur = prev->next;
prev->next = cur->next;
}
cur->next=NULL;
delete cur;
cur=NULL;
}
}
#include "studentlist.h" // class's header file
#include <cstddef>
#include <cassert>
StudentList::StudentList()
{}
StudentList::~StudentList(
{}
bool StudentList::isEmpty() const
{
return size == 0;
}
int StudentList::getLength() const
{
return size;
}
StudentList::Node *StudentList::find(int index) const
{
if ((index < 1) || (index > getLength()))
return NULL;
else
{
Node *cur = head;
for (int skip = 1; skip < index; ++skip)
cur = cur->next;
return cur;
}
}
void StudentList::retreive(int index,ItemType& dataItem) const
{
if ((index < 1) || (index > getLength()))
throw ListIndexOutOfRangeExcepti
else
{
Node *cur = find(index);
dataItem = cur->item;
}
}
void StudentList::insert(int index,ItemType newItem)
{
int newLength = getLength() + 1;
if ((index < 1) || (index > newLength))
throw ListIndexOutOfRangeExcepti
else
{
Node *newPtr = new Node;
if (newPtr == NULL)
throw ListException("ListExcepti
else if(size==MAX)
throw ListException("ListExcepti
else
{
size = newLength;
newPtr->item=newItem;
if (index==1)
{
newPtr->next=head;
head=newPtr;
}
else
{
Node *prev = find(index-1);
newPtr->next=prev->next;
prev->next=newPtr;
}
}
}
}
void StudentList::remove(int index)
{
Node *cur;
if ((index < 1) || (index > getLength()))
throw ListIndexOutOfRangeExcepti
else
{
if (index ==1)
{
cur = head;
head = head->next;
}
else
{
Node *prev = find(index-1);
cur = prev->next;
prev->next = cur->next;
}
cur->next=NULL;
delete cur;
cur=NULL;
}
}
Hmm, I just see that 'insert()' throws an exception of kind "ListException" that was not declared, but 'remove()' seems OK.
ASKER
Excellent, thanks for the example of how to throw an exception with a method. I should have known, I'm a java programmer and I know you have to put throw after the method name. But I'm learning C++, and its the little differences that drive me crazy.
ASKER
#include "ListException.h"
#include "ListIndexOutOfRangeExcept
#include "student.h"
#include <iostream>
#include <list>
typedef student ItemType;
const int MAX=15;
class StudentList
{
public:
StudentList();
~StudentList();
bool isEmpty() const;
int getLength() const;
void insert(int index,ItemType newItem)
throw(ListIndexOutOfRangeE
void remove(int index)
throw(ListIndexOutOfRangeE
void retreive(int index, ItemType& dataItem) const
throw(ListIndexOutOfRangeE
private:
struct Node
{
ItemType item;
Node *next;
};
int size;
Node *find(int index) const;
Node *head;
};