我一直在試圖創建一個實現實時調度算法的程序用於定義的一組處理,同時使用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的類型不起作用,更改返回類型似乎不起作用,而且我的智慧結束了。我在這裏找到了引用類似的東西的答案,但沒有一個解決方案似乎解決了我的問題。任何幫助解決我的錯誤將不勝感激。
參數'os'是一個參考。函數返回一個引用。在'os'之前你不需要'&'。 – LogicStuff
(我正在討論'operator <<',ofc) – LogicStuff