user58504 user58504 - 10 days ago 7
C++ Question

Keeping getting an error of Const PCB cannot convert to *PCB

I am getting an error that says const PCB cannot convert to *PCB and the only way i can declare an object to be NULL is with pointer. Can anyone help me figure out the problem. i put // where the error is happening I'm just trying to store the highest priority Process Control block in the CPU for the first "if statement when it is NULL or empty" and the 2nd compares the top of the priority queue to the cpu and preemptive it if the ready_queue.top is higher than cpu. Have tried using bool isEmpty() and other thing but nothing seems to work

struct PrCB
{
int PrID;
int PrSize;
int prior;
int sumMem= 0;
bool operator < (const PrCB & a)
{
return prior < a.prior;
}
bool operator > (const PrCB & a)
{
return prior > a.prior;
}
};

struct compare
{
bool operator()(const PrCB &a,const PrCB &b)
{
return a.prior < b.prior;
}
};

int main()
{
int pid = 0;
char inter;
PrCB pcb;
PrCB *cpu = NULL;
priority_queue<PrCB, vector<PrCB>,compare> ready_queue;
while(true)
{
cin >> inter;
if(inter == 'N')
{
pcb.PrID = pid;
cout << "How much memory space?" << endl;
cin >> pcb.PrSize;
cout << "What is the Priority?" << endl;
cin >> pcb.prior;
ready_queue.push(pcb);
pid++;
//if(cpu == NULL)
{
cpu == ready_queue.top();
ready_queue.pop();
}
//if(cpu < ready_queue.top())
{
ready_queue.push(cpu);
//cpu = ready_queue.top();
ready_queue.pop();
}
}
}
}

Answer

You need to fix several things with your code and then this will be more clear:

  1. In your operator() overload change PrCBk to PrCB.
  2. In your while loop change While to while.
  3. You do not have operator== defined and this is causing errors whenever you use the == operator on things like cpu.
  4. Be sure to include operator overloads for different data types; i.e. pointers vs. references.
  5. Look at return values for functions like priority_queue.top() and compare const_reference to pointer. Make sure you are using these correctly.

These things need to be corrected so your code compiles. There are other syntax errors as well that need to be corrected.