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日

悬赏问题

  • ¥20 Html备忘录页面制作
  • ¥15 黄永刚的晶体塑性子程序中输入的材料参数里的晶体取向参数是什么形式的?
  • ¥20 数学建模来解决我这个问题
  • ¥15 计算机网络ip分片偏移量计算头部是-20还是-40呀
  • ¥15 stc15f2k60s2单片机关于流水灯,时钟,定时器,矩阵键盘等方面的综合问题
  • ¥15 YOLOv8已有一个初步的检测模型,想利用这个模型对新的图片进行自动标注,生成labellmg可以识别的数据,再手动修改。如何操作?
  • ¥30 NIRfast软件使用指导
  • ¥20 matlab仿真问题,求功率谱密度
  • ¥15 求micropython modbus-RTU 从机的代码或库?
  • ¥15 django5安装失败