2015-11-21 65 views
0

我一直在試圖創建一個實現實時調度算法的程序用於定義的一組處理,同時使用g ++編譯我得到一個錯誤,其中指出:從類型'std :: ostream'初始化類型'std :: ostream&'的非const引用的C++編譯器錯誤

RTSprocess.h:在函數 '的std :: ostream的&操作者< <(標準:: ostream的&,常量rtsProcess &)' : RTSprocess.h84:錯誤:從類型'std :: ostream *'的臨時類型'std :: ostream &'的非const引用無效初始化'

#ifndef RTSPROCESS_H 
#define RTSPROCESS_H 
//defining the rts process 


#include <iostream> 
#include <vector> 
#include <string> 
//include the necessary parts 

using namespace std; 

//create the rts process class itself, declare all necessary variables 
class rtsProcess { 
protected: 
public: 
    int pid; 
    int burst; 
    int arrival; 
    int timeRemaining; 
    int doneWaiting; 
    int finishTime; 
    int deadline; 
    bool failed; 

    //assign base values to all necessary variables 
    rtsProcess() { 
     this->failed = false; 
     this->pid = 0; 
     this->burst = 0; 
     this->arrival = 0; 
     this->timeRemaining =0; 
     this->doneWaiting = 0; 
     this->finishTime = 0; 
     this->deadline = 0; 
    }; 

    //set case where variables assigned by user 
    rtsProcess (int pid, int burst, int arrival, int deadline) { 
     this->pid = pid; 
     this->burst = burst; 
     this->arrival = arrival; 
     this->timeRemaining = burst; 
     this->deadline = deadline; 
     this->doneWaiting = 0; 
     this->finishTime = 0; 
     this->failed = false; 
    }; 
    ~rtsProcess() { 

    }; 
    //set case where input from file 
    rtsProcess(const rtsProcess &p) { 
     pid = p.pid; 
     burst = p.burst; 
     arrival = p.arrival; 
     timeRemaining = p.timeRemaining; 
     deadline = p.deadline; 
     doneWaiting = p.doneWaiting; 
     finishTime = p.finishTime; 
     failed = p.failed; 
    }; 
    // set with return 
    rtsProcess& operator = (const rtsProcess &p) { 
     pid = p.pid; 
     burst = p.burst; 
     arrival = p.arrival; 
     timeRemaining = p.timeRemaining; 
     deadline = p.deadline; 
     doneWaiting = p.doneWaiting; 
     finishTime = p.finishTime; 
     failed = p.failed; 
     return *this; 
    }; 
    //set the operators 
    bool operator== (const rtsProcess &p) { 
     return (this->pid == p.pid && this->arrival == p.arrival && this->burst == p.burst); 
    } 
    bool operator!= (const rtsProcess &p){ 
     return !(this->pid == p.pid && this->arrival == p.arrival && this->burst == p.burst); 
    } 
    friend ostream& operator << (ostream &os, const rtsProcess &p) { 
     p.display(os); 
     return &os; 
    }; 
    //set the display to the console 
    void display(ostream &os) const { 
     os << "\t" << pid; 
     os << "\t" << burst; 
     os << "\t" << arrival; 
     os << "\t" << deadline; 
     os << "\t\t" << timeRemaining; 
    }; 
}; 
#endif 

從我可以告訴它好像錯誤就在於這個代碼塊(也錯誤消息明確地提到它):

friend ostream& operator << (ostream &os, const rtsProcess &p) { 
    p.display(os); 
    return &os; 
}; 

我嘗試,我能想到的一切辦法爲了糾正錯誤,更改傳遞給p.display的類型不起作用,更改返回類型似乎不起作用,而且我的智慧結束了。我在這裏找到了引用類似的東西的答案,但沒有一個解決方案似乎解決了我的問題。任何幫助解決我的錯誤將不勝感激。

+2

參數'os'是一個參考。函數返回一個引用。在'os'之前你不需要'&'。 – LogicStuff

+0

(我正在討論'operator <<',ofc) – LogicStuff

回答

5

變化

friend ostream& operator << (ostream &os, const rtsProcess &p) { 
    p.display(os); 
    return &os; 
}; 

friend ostream& operator << (ostream &os, const rtsProcess &p) { 
    p.display(os); 
    return os; 
}; 

操作&稱爲地址。返回一個引用不同於返回一個產生你的編譯器錯誤的地址。

+0

謝謝,我不敢相信它是如此之小而愚蠢的事情,但它表明我需要大量投入更多時間在C/C++編碼上。 –

+1

沒有「c/C++」這樣的東西。 –

+0

@ KiernanJ.Johnson C++是一隻野獸,你需要有耐心,最終你會到達那裏:)祝你好運! – KostasRim

3

正如在您的文章中的第一個評論所指出的,不要做

return &os; // this creates a temporary pointer to os 

只是做

return os; 

每當你做

&x 

其中x是有些變量會得到一個指向該變量的臨時指針。因此,錯誤消息

RTSprocess.h84: error: invalid initialization of non-const reference of type 'std::ostream&' from a temporary of type 'std::ostream*'

因此編譯器意識到你正試圖返回一個指針,當函數返回的引用,它拋出你的錯誤。

相關問題