从指针队列访问结构的成员
Accessing members of struct from queue of pointers
我在尝试为结构PCB的成员变量赋值时遇到问题。我正在使用指向我的结构的指针队列。因此,我首先取消引用传递给 inititiate_process
函数的指针,然后尝试从ready_queue
引用指针以访问成员变量。如何访问此成员变量?我在这行代码(static_cast<PCB*>(ready_queue->front()))->next_pcb_ptr = &pcb;
上得到了"无效的类型转换"。
这是我在头文件中的结构
#ifndef PCB_H
#define PCB_H
struct PCB {
int p_id;
int *page_table_ptr;
int page_table_size;
int *next_pcb_ptr;
};
#endif // !PCB_H
这是我的源 cpp 文件
#include <iostream>
#include <queue>
#include "PCB.h"
using namespace std;
void initiate_process(queue<int*>* ready_queue) {
// allocate dynamic memory for the PCB
PCB* pcb = new PCB;
// assign pcb next
if(!(ready_queue->empty())){
// get prior pcb and set its next pointer to current
(static_cast<PCB*>(ready_queue->front()))->next_pcb_ptr = &pcb;
}
}
void main(){
queue<int *> ready_queue;
initiate_process(&ready_queue);
}
您确定需要static_cast吗?我建议在你的 PCB.h 中你应该使用
struct PCB *next_pcb_ptr;
然后在程序和initiate_process的主要部分,使用结构 PCB * 而不是 int *
void initiate_process(queue<struct PCB *> *ready_queue) {
// allocate dynamic memory for the PCB
struct PCB *pcb = new struct PCB;
// assign pcb next
if(!(ready_queue->empty())){
(ready_queue->front())->next_pcb_ptr = pcb;
}
}
相关文章:
- 如何在C++中定义静态成员结构
- 使用模板化类的成员结构
- 初始化固定的 C 数组成员结构
- 指向成员结构的指针如何工作
- 如何将此“指针从外部类传递到成员结构
- 导致IAR ARM中出现错误的成员结构位字段元素的Initializer列表初始化
- 映射文件中成员结构的地址
- Visual C++ 模板类成员结构初始化语法糖
- C :初始化成员结构的静态字段的正确方法
- 如何在C 类的初始化器列表中使用未命名结构初始化成员结构
- 从成员结构的成员函数中访问类的成员?
- 模板化类的成员函数无法返回指向成员结构的指针?
- 重载非类型模板结构的成员结构的复制赋值运算符
- 灵气属性传播问题与单成员结构.
- 将具有 int* 成员C++结构编接到 C#
- 复杂的班级成员结构
- 将向量成员(结构)传递给函数
- 成员结构的前向声明
- 重载运算符<<用于模板结构中定义的成员结构
- 返回带有模板化类的类成员结构