stubborn Stack and Queue

This is a discussion on stubborn Stack and Queue within the C++ Programming forums, part of the General Programming Boards category; Hi all, Here's the deal, I have two data structures ( a stack and a queue ) implementing in a ...

  1. #1
    Registered User
    Join Date
    Sep 2010
    Posts
    10

    Thumbs down stubborn Stack and Queue

    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

  2. #2
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,703
    Your stack and queue classes are abstract base classes. It is because they are abstract that you get the error "expected constructor, destructor, or type conversion before ; token". You can't make an object of an abstract base class. The base has no implementation apart from its child classes. Use one of the child classes as object types.

  3. #3
    Registered User
    Join Date
    Sep 2010
    Posts
    10

    Question

    Quote Originally Posted by whiteflags View Post
    Use one of the child classes as object types.
    Ok, I understand. I had a feeling that was it. But what do you mean by that? Because if I declare them
    Code:
     arrayStack<int> stack_;
    My compiler still complains:

    maze.cpp:14: error: declaration does not declare anything
    maze.cpp:15: error: declaration does not declare anything

  4. #4
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,703
    Did you implement your constructors for arrayQueue and arrayStack completely?

  5. #5
    Registered User
    Join Date
    Sep 2010
    Posts
    10
    how so?

    these are the contructors:
    Stack:
    Code:
    template<class T>
    
    arrayStack<T>::arrayStack(int initialCapacity)
    
    {// Constructor.
    
       if (initialCapacity < 1)
    
       {ostringstream s;
    
        s << "Initial capacity = " << initialCapacity << " Must be > 0";
    
        throw illegalParameterValue(s.str());
    
       }
    
       arrayLength = initialCapacity;
    
       stack = new T[arrayLength];
    
       stackTop = -1;
    
    }
    Queue:
    Code:
    template<class T>
    
    arrayQueue<T>::arrayQueue(int initialCapacity)
    
    {// Constructor.
    
       if (initialCapacity < 1)
    
       {ostringstream s;
    
        s << "Initial capacity = " << initialCapacity << " Must be > 0";
    
        throw illegalParameterValue(s.str());
    
       }
    
       arrayLength = initialCapacity;
    
       queue = new T[arrayLength];
    
       theFront = 0;
    
       theBack = 0;
    
    }
    but I want to set stack_ = new arrayStack(width_); inside different funcitons depending on the type of search I want.

  6. #6
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,703
    Quote Originally Posted by federico View Post
    how so?
    In what you posted before, you wrote:
    arrayStack(int initialCapacity = 10);

    arrayQueue(int initialCapacity = 10);

    These are not complete implementations.

    these are the contructors:
    OK, those would make them complete. In what files are those? You can't really separate a template from its implementation without complicated work. It all has to be in the h file.

    but I want to set stack_ = new arrayStack(width_); inside different funcitons depending on the type of search I want.
    stack_ needs to be a pointer.

  7. #7
    Registered User
    Join Date
    Sep 2010
    Posts
    10
    OMG! I"m retarded!!

    Ofcourse they need to be pointers!!

    And yes, those constructors belong to the same header files.
    You're a life saver dawg!

  8. #8
    Registered User
    Join Date
    Sep 2010
    Posts
    10
    Also, I'm a little confused now on how I should call the functions for those pointers.
    suppose I declare now:

    Code:
    stack_ = new arrayStack(width_);
    and then later on I want to return a stack_.empty(); .... obviously, that wouldn't work...
    neither does *stack_.empty() ... .soooo..... how exactly is this supposed to work?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. queue and stack implemented with linked list
    By popapez in forum C Programming
    Replies: 14
    Last Post: 09-22-2009, 10:56 PM
  2. Fixing my program
    By Mcwaffle in forum C Programming
    Replies: 5
    Last Post: 11-05-2008, 02:55 AM
  3. Pushing a Queue onto a Stack?
    By MiroMage in forum C Programming
    Replies: 5
    Last Post: 10-14-2008, 09:23 PM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 09:33 AM
  5. queue / stack
    By smd in forum C++ Programming
    Replies: 2
    Last Post: 07-26-2002, 01:30 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21