c++ - 如何在 C++ 中正确地将一个类的头文件包含在另一个类的头文件中?

标签 c++ stack queue include-path

我正在尝试使用两个堆栈实现一个队列,因此我已经创建了一个功能齐全的类堆栈。创建队列头文件时,我包含了堆栈头文件。然后我将 queue.h 包含在我的 queue.cpp 文件中。但是,当我编写使用堆栈对象的入队函数时,堆栈对象不可见,我不知道为什么。我做错了什么?

堆栈.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);

};

队列.h

#include "stack.h"

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


    private:
        stack s1;
        stack s2;

};

队列.cpp

#include "queue.h"


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

最佳答案

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

应该是

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

关于c++ - 如何在 C++ 中正确地将一个类的头文件包含在另一个类的头文件中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29653608/

相关文章:

c++ - atof 只返回整数?

c++ - 从 char* 缓冲区构造字符串时是否需要考虑编码 (UTF-8)

c++ - 是否可以在不使用指针的情况下将变量的范围扩展到 if 语句之外?

android - 如何正确删除所有 Activity 堆栈?

C++ 将后缀转换为中缀

java - 使用消息使用者时的 JMS 自动确认

c++ - 我从源代码构建的 SDL 库崩溃了!

c++ - 在巨大的棋盘上解决骑士的巡回赛问题?

python - 我在 Django 应用程序中的哪里注册 rq-scheduler 作业?

c++ - 使用数组和 bool 值在输出未知时从输出中删除逗号