LLJJCC66 2021-07-28 14:16 采纳率: 0%
浏览 24

C++编写头文件(.h)时,定义了一个类,数据成员是两个栈的类型(就是用两个栈实现一个队列),我应该在哪个文件头部#include<stack>呢?

主函数main.cpp代码如下:

//main函数
#include <iostream>
//#include <stack>
#include "myQueue.h"
using namespace std; 

int main() {
    myQueue test;
    cout<<test.dele();
    cout<<endl;
    test.add(5);
    cout<<endl;
    test.add(2);
    cout<<endl;
    cout<<test.dele();
    cout<<endl;
    cout<<test.dele();
    cout<<endl;
    return 0;
}


myQueue.h代码:

//队列类头文件

//#include<stack>

#ifndef MY_QUEUE_H
#define MY_QUEUE_H

//#include<stack>

class myQueue {
    private:
        stack<int> tail;
        stack<int> head;
    public:
        void add(int);
        int dele();
};

#endif

两个成员函数的实现myQueue.cpp如下:

//队列类
#include<stack>
#include "myQueue.h"


void myQueue::add(int x) {
    tail.push(x);
}

int myQueue::dele() {
    int data;
    if(!head.empty()) {
        data=head.top();
        head.pop();
        return data;
    } 
    else {
        if(!tail.empty()) {
            while(!tail.empty()) {
                head.push(tail.top());
                tail.pop();
            }
            data=head.top();
            head.pop();
            return data;
        } 
        else {
            //cout<<"The queue is empty"<<endl;
            return -1;
        }
    }
}//end 

显示的报错信息:

img

我尝试在这三个文件中都#include过,但是还是出错。

  • 写回答

2条回答 默认 最新

  • 快乐鹦鹉 2021-07-28 14:18
    关注

    哪里用到,就在哪里#include
    根据上述代码,应该在myQueue.h
    用#include "stack.h"

    评论

报告相同问题?

问题事件

  • 创建了问题 7月28日

悬赏问题

  • ¥15 python怎么在已有视频文件后添加新帧
  • ¥20 虚幻UE引擎如何让多个同一个蓝图的NPC执行一样的动画,
  • ¥15 fluent里模拟降膜反应的UDF编写
  • ¥15 MYSQL 多表拼接link
  • ¥15 关于某款2.13寸墨水屏的问题
  • ¥15 obsidian的中文层级自动编号
  • ¥15 同一个网口一个电脑连接有网,另一个电脑连接没网
  • ¥15 神经网络模型一直不能上GPU
  • ¥15 pyqt怎么把滑块和输入框相互绑定,求解决!
  • ¥20 wpf datagrid单元闪烁效果失灵