c++ - 嵌套容器:为什么我不能访问堆栈队列顶部的堆栈?C++
问题描述
所以我使用 STL 创建了一个堆栈队列,如下所示:
void main()
{
queue<stack<string>> qos;
stack<string> words;
words.push("hey");
qos.push(wors);
cout<< (qos.pop()).top()<<endl;
}
预期行为:
返回单词嘿
实际结果:
错误:成员引用基类型“void”不是结构或联合
我的问题是为什么它不返回我所期望的,我的意思是因为 qos.pop() 返回堆栈元素并且堆栈具有成员函数 top();
解决方案
你的变量words
和qos
没有任何关系。
还main()
必须有一个返回类型int
。
您收到错误消息的原因是,可以调用queue<>::pop
不返回值的错误消息。top()
你可能想要
#include <iostream>
#include <queue>
#include <stack>
#include <string>
int main()
{
std::queue<std::stack<std::string>> qos;
std::stack<std::string> words;
words.push("hey");
qos.push(words);
std::cout << qos.front().top() << '\n';
}
推荐阅读
- sql - 从其他两个列值的条件中减去两列
- python - 请问如何通过 pypptx 或 win32com 重命名幻灯片母版
- firebase - 在 Firebase 中获取 API 调用的空对象列表
- pgp - 如何将现有的 OpenPGP 密钥导入 Claws 邮件?
- sapui5 - 我的 UI5 应用程序中未显示数据绑定文本
- r - 大数据集的降维方法和聚类算法
- java - 带有 mongoTemplate 的 DbRef 字段的 Mongo 聚合
- arrays - 将文本文件的内容添加到 Bash 中的数组
- c++ - 如何在构造函数中记录时间以及如何从 C++ 结构的析构函数中的构造中减去当前时间?
- node.js - “npm install *”中的非法指令