2016-11-27 17 views
0

我得到的是说const的PCB不能转换的错误* PCB,我可以声明对象为NULL的唯一方法是使用指针。任何人都可以帮我弄清楚这个问题。我把/ /错误发生的地方我只是试图存储CPU中的最高优先级过程控制块为第一个“如果语句时,它是NULL或空”,第二个比较优先级队列的顶部cpu,如果ready_queue.top高于cpu,则抢占它。使用试过布尔的isEmpty()和其他的事情,但似乎没有任何工作饲养越来越常量PCB的错误不能转换为* PCB

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; 
    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(); 
      } 
     } 
    } 
} 

回答

0

您需要解决几件事情与您的代码,然后这将是更为清晰:

  1. 在你operator()过载变化PrCBkPrCB
  2. 在while循环变化Whilewhile
  3. 你不必operator==定义,只要您使用上的东西==运营商像cpu这是造成错误。
  4. 一定要包含不同的数据类型运算符重载;即指针与参考。
  5. 看像priority_queue.top()函数的返回值,并比较const_reference到指针。确保你正确使用这些。

这些东西需要加以纠正所以你的代码编译。还有其他语法错误需要更正。

+0

因此,如果我要做一个布尔运算符==(PrCB&a)如果(a.priority == NULL)返回一些东西?我不明白你的意思是由priority_queue.top()或你的意思是ready_queue.top()?谢谢您的帮助 ! – user58504

+0

'a.priority'的类型为'int',这意味着您不需要检查'NULL'。我认为你可能会对什么是指针和什么不是,“int”不是指针感到困惑。 'ready_queue'的类型是'priority_queue',所以你可以读作'ready_queue.top()'。 – user2205930

+0

ahhh好,所以如果我们使布尔运算符==(PrCB&a)那是假设返回什么因为我想要使用if(cpu ==空或NULL)将ready_queue.top()移动到cpu,这是我有什么麻烦:S – user58504