2016-11-27 24 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