我正在尝试使用两个堆栈实现一个队列,因此我已经创建了一个功能齐全的类堆栈。创建队列头文件时,我包含了堆栈头文件。然后我将 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/