exceptions problem just dont understand
Dec 2, 2011 at 6:50pm UTC
The real problem is i just don't understand exceptions at all. Here is what I got it is a stack program.
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
/**
* Passes the top tag from the stack
* @param me, tag to be returned
* @throws StackException
*/
void TagStack::get(Tag &me) throw (StackException) {
if (!isEmpty()) {
me = top->tag;
}
throw StackException("StackException: Stack is empty can't get." );
}
/**
* Takes the top tag off the stack
* @throws StackException.
*/
void TagStack::pop() throw (StackException) {
if (!isEmpty()) {
TagStackNode *temp;
temp = top;
top = temp->next;
delete temp;
}
throw StackException("StackException: Stack is empty can't pop." );
}
/**
* Puts a new tag on top of the stack
* @param me, tag to be put on top
* @throws StackException
*/
void TagStack::push(Tag me) throw (StackException) {
try {
TagStackNode *node = new TagStackNode;
node->tag = me;
node->next = top;
top = node;
}catch (StackException &e){
throw StackException("StackException: push can't allocate memory." );
}
}
on each throw line netbeans gives me this.
/home/bullfrog/NetBeansProjects/CnCpp/XmlValidator/TagStack.cpp:69: undefined reference to `StackException::StackException(std::basic_string<char, std::char_traits<char>, std::allocator<char> >)'
here is my stackexception.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
/********************************************
* Project: XmlValidator
* File: StackException.h
* Author: Jeremiah Johnson
* Assignments: Stack
* Date: November 15, 2011
********************************************/
#ifndef STACKEXCEPTION_H
#define STACKEXCEPTION_H
#include <exception>
#include <string>
#include <stdexcept>
using namespace std;
class StackException: public exception {
public :
StackException(); throw ();
StackException(string explain) throw ();
virtual const char * what() const throw ();
};
#endif /* STACKEXCEPTION_H */
i really need a better understanding of exceptions and what might be missing from my program
Dec 2, 2011 at 6:53pm UTC
did you include StackException.h?
Also you have a bug in your get and pop function. They will always throw.
Dec 2, 2011 at 8:45pm UTC
yes i did include StackException.h and i changed the pop to say
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/**
* Takes the top tag off the stack
* @throws StackException.
*/
void TagStack::pop() throw (StackException) {
if (!isEmpty()) {
TagStackNode *temp;
temp = top;
top = temp->next;
delete temp;
} else {
throw StackException("StackException: Stack is empty can't pop." );
}
}
that should fix the allways throw u were mentioning thanks any other ideas.
Dec 2, 2011 at 8:50pm UTC
The undefined reference error says it can't find the definition of the StackException constructor. Did you define it somewhere?
Dec 3, 2011 at 4:09am UTC
I think i did that is what i mean by i don't understand exceptions yet i have the .h file right here.
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
/********************************************
* Project: XmlValidator
* File: StackException.h
* Author: Jeremiah Johnson
* Assignments: Stack
* Date: November 15, 2011
********************************************/
#ifndef STACKEXCEPTION_H
#define STACKEXCEPTION_H
#include <exception>
#include <string>
#include <stdexcept>
using namespace std;
class StackException: public exception {
public :
StackException(); throw ();
StackException(string explain) throw ();
virtual const char * what() const throw ();
};
#endif /* STACKEXCEPTION_H */
or do I need to do it more like any other variable like this
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/**
* Takes the top tag off the stack
* @throws StackException.
*/
void TagStack::pop() throw (StackException) {
StackException exception;
if (!isEmpty()) {
TagStackNode *temp;
temp = top;
top = temp->next;
delete temp;
} else {
throw StackException("StackException: Stack is empty can't pop." );
}
}
Thanks for the tip let me see if that works thanks.
Dec 3, 2011 at 4:50am UTC
Thanks for the tip that worked now i just need to know what goes in to an StackException.h file I have no idea what to put in it.
Dec 3, 2011 at 4:52am UTC
any ideas would be a great help. Thanks.
Dec 3, 2011 at 5:44am UTC
ok that didnt work eather really i thought it did but now i am getting the same error that i was getting on the throw line on the line 6 of the above code I am just plain confused here is the source code for my current StackException.h file and the TagStack.cpp and TagStack.h
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 152 153 154 155 156 157 158 159 160 161 162
/***********************************
* Project: XmlValidator
* File: TagStack.h
* Author: Jeremiah Johnson
* Assignment: Stacks
* Date: November 3, 2011
***********************************/
#ifndef TAGSTACK_H
#define TAGSTACK_H
#include <istream>
#include "Tag.h"
#include "TagStackNode.h"
#include "StackException.h"
#include <exception>
using namespace std;
class TagStack {
public :
TagStack();
~TagStack();
bool isEmpty();
void push(Tag me) throw (StackException);
void pop() throw (StackException);
void get(Tag &me) throw (StackException);
private :
TagStackNode *top;
};
#endif /* TAGSTACK_H */
/********************************************
* Project: XmlValidator
* File: StackException.h
* Author: Jeremiah Johnson
* Assignments: Stack
* Date: November 15, 2011
********************************************/
#ifndef STACKEXCEPTION_H
#define STACKEXCEPTION_H
#include <exception>
#include <string>
#include <stdexcept>
using namespace std;
class StackException: public exception {
public :
StackException() throw ();
StackException(string explain) throw ();
virtual const char * what() const throw ();
};
#endif /* STACKEXCEPTION_H */
/***********************************
* Project: XmlValidator
* File: TagStack.cpp
* Author: Jeremiah Johnson
* Assignment: Stacks
* Date: November 3, 2011
***********************************/
#include "TagStack.h"
#include "TagStackNode.h"
#include "Tag.h"
#include <string>
#include <istream>
#include <fstream>
#include "StackException.h"
#include <new>
#include <stdexcept>
using namespace std;
/**
* Constructs an empty stack
*/
TagStack::TagStack() {
top = NULL;
}
/**
* Destroys the stack
*/
TagStack::~TagStack() {
while (top != NULL) {
pop();
}
}
/**
* Returns weather or not the stack is empty
* @return bool, true if stack is empty
*/
bool TagStack::isEmpty() {
return top == NULL;
}
/**
* Passes the top tag from the stack
* @param me, tag to be returned
* @throws StackException
*/
void TagStack::get(Tag &me) throw (StackException) {
StackException except;
if (!isEmpty()) {
me = top->tag;
} else {
throw except;
}
}
/**
* Takes the top tag off the stack
* @throws StackException.
*/
void TagStack::pop() throw (StackException) {
StackException except;
if (!isEmpty()) {
TagStackNode *temp;
temp = top;
top = temp->next;
delete temp;
} else {
throw except;
}
}
/**
* Puts a new tag on top of the stack
* @param me, tag to be put on top
* @throws StackException
*/
void TagStack::push(Tag me) throw (StackException) {
StackException except;
try {
TagStackNode *node = new TagStackNode;
node->tag = me;
node->next = top;
top = node;
} catch (StackException &e) {
throw except;
}
}
Any help would be great thanks in advance.
Dec 6, 2011 at 9:50pm UTC
All working good now Source code is avalible upon request
Topic archived. No new replies allowed.