简体   繁体   中英

How to correctly include a class's header file in another class's header file in C++?

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);
}
void enqueue(int item)
{
   s1.push(item);
}

should be

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM