在队列中推动结构变量
pushing structure variable in queue
#include <iostream>
#include <queue>
using namespace std;
int main () {
struct process {
int burst;
int ar;
};
int x=4;
process a[x];
queue <string> names; /* Declare a queue */
names.push(a[1]);
return 0;
}
我正在尝试在队列中推动结构变量,但它不采取并给出错误
no matching function for #include queue and invalid argument
我该怎么做?
c 是一种强烈打字的语言。在names.push(a[1]);
行中,您正在尝试将struct
(从process a[x];
数组)推入queue<string>
。您的结构不是string
,因此编译器会发出错误。您至少需要queue<process>
。
其他问题:可变长度阵列不是标准的C (process a[x];
)。改用std::vector<process>
。这是一些有效的简单示例:
#include <iostream>
#include <queue>
#include <string>
#include <vector>
using namespace std;
int main () {
struct process // move this outside of main() if you don't compile with C++11 support
{
int burst;
int ar;
};
vector<process> a;
// insert two processes
a.push_back({21, 42});
a.push_back({10, 20});
queue <process> names; /* Declare a queue */
names.push(a[1]); // now we can push the second element, same type
return 0; // no need for this, really
}
编辑
用于实例化模板的局部定义的类/结构仅在C 11和更高版本中有效,请参见例如为什么我可以在C 中的功能中定义结构和类?和内部的答案。如果您无法访问C 11符合编译器,请在main()
之外移动struct
定义。
相关文章:
- 在类构造函数中使用结构变量
- 在类构造函数中定义结构变量的参数
- 为什么我们再次从结构对象创建结构变量?
- 如何从其他结构访问受保护的结构变量
- 指向结构变量的指针未保存到eeprom
- 为什么我的程序没有更改结构变量中的值?
- 在 CPP 中初始化 typedef 结构变量
- 带有大结构变量的 CUDA 内核函数给出了错误的结果
- 有没有办法初始化不涉及编写构造函数的新结构变量?
- 结构变量查询
- 在结构声明的分号之前声明的结构变量是否为全局变量
- C++ 如何访问指针的结构变量
- 使用指向结构变量成员的指针访问该结构的成员的地址
- 为什么在 sizeof() 函数中与 * 运算符一起使用和不使用 * 运算符时,指向结构变量的指针大小会有所不同?
- 如何在类别内的类中访问类中的结构变量
- 尝试使用文本文件初始化多个结构变量
- 从类对象到结构变量的 memcpy
- C++结构变量初始化时出现问题
- 如何在 C++ 中使用带有类/结构变量的列表
- 动态数组添加元素而不创建结构变量/对象