0

I am trying to implement a queue using two stacks so I have already made a fully functioning class stack. When creating the queue header file, I included the stack header file. And then I included queue.h in my queue.cpp file. However, when I was writing my enqueue function, which uses a stack object, the stack object wasn't visible and I don't know why. What am I doing wrong?

stack.h

using namespace std;

class stack
{

    public:
        stack(); //constructor
        ~stack();//deconstructor
        void push(int); //puts int on the stack
        void pop(); //removes int off the stack

        void printStack();



    private:
        struct Node
        {
            int number;
            Node* prev;

        };

        Node* top;
        void readNode(Node* r);

};

queue.h

#include "stack.h"

class queue
{
    public:
        queue();
        virtual ~queue();
        void enqueue(int item);
        void dequeue();
        bool isEmpty() const;


    private:
        stack s1;
        stack s2;

};

queue.cpp

#include "queue.h"


void enqueue(int item)
{
   s1.push(item);
}
LePerMu
  • 77
  • 1
  • 7
  • Get rid of that `using namespace std`. It's a [bad idea](http://stackoverflow.com/questions/1452721) in general, doubly so if you want to declare things like `stack` or `queue` with names used by the standard library. – Mike Seymour Apr 15 '15 at 15:15

1 Answers1

1
void enqueue(int item)
{
   s1.push(item);
}

should be

void queue::enqueue(int item)
{
   s1.push(item);
}
dwcanillas
  • 3,562
  • 2
  • 24
  • 33
  • Oops, I did have this before, I don't know what happened when I copied the code to StackOverflow, but regardless of this, s1 is still not visible. I get an error saying "s1 is not declared in this scope" and I don't know why? – LePerMu Apr 15 '15 at 15:06
  • @LePerMu with what you have posted, the suggestion I made should solve your problem. If you are still having problems, you need to give more details. – dwcanillas Apr 15 '15 at 15:13
  • Yes I am still having problems. I know I made a mistake while posting, I know the correct syntax for defining a function is exactly what you mentioned (using the scope operator ::) But even so, when I write the function like void queue::enqueue(int item) and then go and try to use s1 in the body, s1 is not "seen". When I compile, I get this error message "s1 was not declared in this scope". I don't understand why because I have included all the necessary header files. – LePerMu Apr 15 '15 at 15:42
  • @LePerMu The code you have here compiles fine. You either need to edit your post to have the code with the problem so it is reproducable. – dwcanillas Apr 15 '15 at 15:54
  • it is not compiling on my machine. I am using Codeblocks, could it be the IDE? – LePerMu Apr 15 '15 at 16:12