Reputation: 564
I am having an issue trying to assign a value to a member variable of my struct, PCB. I am using a queue of pointers to my struct. So I first dereference the pointer passed to the inititiate_process
function, and next try to deference the pointer from the ready_queue
to access the member variable. How do I access this member variable? I am getting an 'invalid type conversion' on this line of code (static_cast<PCB*>(ready_queue->front()))->next_pcb_ptr = &pcb;
.
Here is my struct in a header file
#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
Here is my source cpp file
#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);
}
Upvotes: 1
Views: 1070
Reputation: 156
Are you sure you need the static_cast? I suggest in your PCB.h you should instead use
struct PCB *next_pcb_ptr;
and then in the main part of the program and initiate_process, use struct PCB * instead of 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;
}
}
Upvotes: 1