我在尝试为我的结构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);
}
发布于 2018-02-04 07:49:59
您确定需要static_cast吗?我建议您在PCB.h中使用
struct PCB *next_pcb_ptr;
然后在程序和initiate_process的主要部分,使用struct 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;
}
}
https://stackoverflow.com/questions/48603105
复制相似问题