- #1
- 2,593
- 5
I'm studying for an introductory C++ programming course and I'm trying to write the code for a doubly-circular linked list. To keep it simple, I had written my list to be tailored to that of a wedding dinner guest invitation list. Hence I didn't use templates at all. I've attempted to compile this code, but for some reason I just couldn't get it to compile because of some error due to exception handling.
This is the error I get(among a lot of other errors, so I figured I'll fix this one at a time, so for now just focus on the red text):
As you can see, most of the errors are related to the 'exception' class I've defined and included in the header file. So I figured if I fix that error, most of the other errors would disappear as well.
This is the contensts of 'weddinglist.h':
Note: The red text indicates the lines for which the compiler error message is displayed for.
Contents of "exception.h":
Contents of 'weddinglistimp.cpp'. This is the implementation file for the above-mentioned member functions of the doubly-circular linked list:
Now the question I have here is this: Why does the compiler say that 'exception' is undefined when I have so defined it in a user-defined header file and included it? Furthermore, why does it say (highlighted as blue text in the first CODE block) that a class redefinition has been attempted? I've only defined the class 'exception' once.
This is the error I get(among a lot of other errors, so I figured I'll fix this one at a time, so for now just focus on the red text):
Code:
In file included from weddinglist.h:2,
from weddinglistimp.cpp:2:
[COLOR="Blue"]exception.h:4: error: redefinition of `class exception'
exception.h:4: error: previous definition of `class exception'[/COLOR]
In file included from weddinglistimp.cpp:2:
[COLOR="Red"]weddinglist.h:20: error: `exception' has not been declared
weddinglist.h:22: error: `exception' has not been declared
weddinglist.h:24: error: `exception' has not been declared[/COLOR]
weddinglistimp.cpp:40: error: `exception' has not been declared
weddinglistimp.cpp: In member function `void weddinglist::insert(int, const relation&, const std::string&)':
weddinglistimp.cpp:44: error: `exception' undeclared (first use this function)
weddinglistimp.cpp:44: error: (Each undeclared identifier is reported only once for each function it appears in.)
weddinglistimp.cpp: At global scope:
weddinglistimp.cpp:82: error: `exception' has not been declared
weddinglistimp.cpp: In member function `void weddinglist::remove(int)':
weddinglistimp.cpp:86: error: `exception' undeclared (first use this function)
weddinglistimp.cpp: At global scope:
weddinglistimp.cpp:110: error: `exception' has not been declared
weddinglistimp.cpp: In member function `void weddinglist::retrieve(int, relation&, std::string&)':
weddinglistimp.cpp:113: error: `exception' undeclared (first use this function)
weddinglistimp.cpp: At global scope:
weddinglistimp.cpp:121: error: `exception' has not been declared
weddinglistimp.cpp:121: error: ISO C++ forbids declaration of `exception' with no type
weddinglistimp.cpp: In function `int exception(std::string)':
weddinglistimp.cpp:121: error: `int exception(std::string)' used prior to declaration
weddinglistimp.cpp:122: error: `_message' undeclared (first use this function)
weddinglistimp.cpp: At global scope:
weddinglistimp.cpp:124: error: `exception' has not been declared
weddinglistimp.cpp:124: error: ISO C++ forbids declaration of `exception' with no type
weddinglistimp.cpp: In function `int exception()':
weddinglistimp.cpp:125: error: `_message' undeclared (first use this function)
weddinglistimp.cpp: At global scope:
weddinglistimp.cpp:127: error: `exception' has not been declared
weddinglistimp.cpp:127: error: non-member function `std::string getmessage()' cannot have `const' method qualifier
weddinglistimp.cpp: In function `std::string getmessage()':
weddinglistimp.cpp:128: error: `_message' undeclared (first use this function)
This is the contensts of 'weddinglist.h':
Note: The red text indicates the lines for which the compiler error message is displayed for.
Code:
#include <string>
#include "exception.h"
using namespace std;
enum relation { pal, coworker, relative };
class weddinglist{
public:
//Constructor, destructor
weddinglist();
~weddinglist();
//Small functions
bool isempty() const;
int getlength() const;
//Big functions
void insert(int index, const relation& type, const string &name)
[COLOR="Red"] throw (exception);[/COLOR]
void remove(int index)
[COLOR="red"]throw (exception);[/COLOR]
void retrieve(int index, relation& type, string& name)
[COLOR="red"]throw (exception);[/COLOR]
private:
struct node{
string _name;
relation _type;
node *next, *prev;};
int size;
node *head;
node* find(int index) const;
};
Contents of "exception.h":
Code:
#include <string>
using namespace std;
class exception{
public:
exception(string errmessage);
exception();
string getmessage() const;
private:
string _message;
};
Contents of 'weddinglistimp.cpp'. This is the implementation file for the above-mentioned member functions of the doubly-circular linked list:
Code:
#include "exception.h"
#include "weddinglist.h"
#include <string>
#include <iostream>
using namespace std;
weddinglist::weddinglist()
:size(0), head(NULL){}
weddinglist::~weddinglist()
{
while (!isempty())
remove(1);
} // end destructor
bool weddinglist::isempty() const
{
return size == 0;
} // end isempty
int weddinglist::getlength() const
{
return size;
} // end getLength
weddinglist::node *weddinglist::find(int index) const
{
if ( (index < 1) || (index > getlength()) )
return NULL;
else // count from the beginning of the list.
{
node *cur = head;
for (int skip = 1; skip < index; ++skip)
cur = cur->next;
return cur;
} // end if
} // end find
void weddinglist::insert(int index, const relation& type, const string& name)
throw (exception)
{
if( (index<1)||(index>(getlength()+1)))
throw exception("Bad index specified");
else{
cout<<"Line 42"<<endl;
node *ptr = new node;
size++;
cout<<"Line 45"<<endl;
cout<<size<<endl;
ptr->_name = name;
ptr->_type = type;
cout<<"Line 48"<<endl;
if((index==1) || (index==size))
{
node *last = find(size-1);
cout<<"Line 53"<<endl;
ptr->next = head;
ptr->prev = last;
cout<<"Line 56"<<endl;
if(size!=1){
head->prev = ptr;
cout<<"Line 58"<<endl;
last->next = ptr;
cout<<"Line 59"<<endl;}
head = ptr;}
else{
node *cur = find(index);
ptr->next = cur->next;
ptr->prev = cur;
node *nextnode = cur->next;
nextnode->prev = ptr;}
}
}
void weddinglist::remove(int index)
throw (exception)
{
if ( (index<1)||(index>getlength()))
throw exception("Bad index specified");
else
{
size--;
if (index==1){
node *last = find(index);
node *cur = head->next;
cur->prev = last;
last->next = cur;
delete head;
head = cur;}
else{
node *cur = find(index);
node *nodeptr = cur->prev;
nodeptr->next = cur->next;
nodeptr = cur->next;
nodeptr->prev = cur->prev;
delete cur;
cur = NULL;}
}
}
void weddinglist::retrieve(int index, relation& type, string& name)
throw (exception)
{
if ( (index<1) || (index>getlength()) )
throw exception("Bad index specified");
else{
node *cur = find(index);
type = cur->_type;
name = cur->_name;}
}
exception::exception(string errmessage){
_message = errmessage;}
exception::exception(){
_message = "Out of index error default";}
string exception::getmessage() const{
return _message;}
Now the question I have here is this: Why does the compiler say that 'exception' is undefined when I have so defined it in a user-defined header file and included it? Furthermore, why does it say (highlighted as blue text in the first CODE block) that a class redefinition has been attempted? I've only defined the class 'exception' once.