Hi all,
Here's the deal, I have two data structures ( a stack and a queue ) implementing in a "rat in a maze" classic solver.
for the sake of the reader and not to make this thread extra long, I will post how my virtual functions and declarations of each of my stack and queue header files look like and you'll get the idea...
Stack header:
Code:template<class T> class stack { public: virtual ~stack() {} virtual bool empty() const = 0; // return true iff stack is empty virtual int size() const = 0; // return number of elements in stack virtual T& top() = 0; // return reference to the top element virtual void pop() = 0; // remove the top element virtual void push(const T& theElement) = 0; // insert theElement at the top of the stack }; template<class T> class arrayStack : public stack<T> { public: arrayStack(int initialCapacity = 10); ~arrayStack() {delete [] stack;} bool empty() const {return stackTop == -1;} int size() const {return stackTop + 1;} T& top() { if (stackTop == -1) throw stackEmpty(); return stack[stackTop]; } void pop() { if (stackTop == -1) throw stackEmpty(); stack[stackTop--].~T(); // destructor for T } void push(const T& theElement); private: int stackTop; // current top of stack int arrayLength; // stack capacity T *stack; // element array };
Queue header:
Code:template<class T> class queue { public: virtual ~queue() {} virtual bool empty() const = 0; // return true iff queue is empty virtual int size() const = 0; // return number of elements in queue virtual T& front() = 0; // return reference to the front element virtual T& back() = 0; // return reference to the back element virtual void pop() = 0; // remove the front element virtual void push(const T& theElement) = 0; // add theElement at the back of the queue }; template<class T> class arrayQueue : public queue<T> { public: arrayQueue(int initialCapacity = 10); ~arrayQueue() {delete [] queue;} bool empty() const {return theFront == theBack;} int size() const {return (theBack - theFront + arrayLength) % arrayLength;} T& front() {// return front element if (theFront == theBack) throw queueEmpty(); return queue[(theFront + 1) % arrayLength]; } T& back() {// return theBack element if (theFront == theBack) throw queueEmpty(); return queue[theBack]; } void pop() {// remove theFront element if (theFront == theBack) throw queueEmpty(); theFront = (theFront + 1) % arrayLength; queue[theFront].~T(); // destructor for T } void push(const T& theElement); private: int theFront; // 1 counterclockwise from theFront element int theBack; // position of theBack element int arrayLength; // queue capacity T *queue; // element array };
My concern is that, in my .cpp file I have the following global declarations:
Code:#define WIDTH 15 #define HEIGHT 15 int maze_ [WIDTH][HEIGHT]; int width_; int height_; stack stack_; // must use this variable to refer your stack queue queue_; // must use this variable to refer your queue int traveled_; // must use this variable to store your pop actions int pathLength_; // must use this variable to store the length of the path
My concern is that those stack_ and queue_ variables are not playing nice with the compiler that way I have them declared.
I get this error:
maze.cpp:14: error: expected constructor, destructor, or type conversion before ‘;’ token
maze.cpp:15: error: expected constructor, destructor, or type conversion before ‘;’ token
I tried declaring them as simple arrayStack<int> stack_ and same for queue, but that doesn't work either, and its not what I really want.
Any help??
Thanks for reading and I appreciate any feedback,
Fed



LinkBack URL
About LinkBacks




