gpt4 book ai didi

关于从 'std::ostream&' 类型的临时对象初始化 'std::ostream"类型的非常量引用的 C++ 编译器错误

转载 作者:太空宇宙 更新时间:2023-11-04 15:01:54 26 4
gpt4 key购买 nike

我一直在尝试创建一个程序来为一组已定义的进程实现实时调度算法。使用 g++ 编译时出现错误,其中指出:

RTSprocess.h:在函数“std::ostream& operator<<(std::ostream&, const rtsProcess&)”中:RTSprocess.h84:错误:从类型为“std::ostream*”的临时类型中对类型为“std::ostream&”的非常量引用的初始化无效

#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 的类型不起作用,更改返回类型似乎也不起作用,我有点在我的束手无策。我在这里找到了引用类似内容的答案,但没有一个解决方案似乎可以解决我的问题。非常感谢任何帮助解决我的错误。

最佳答案

改变

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

运算符&被称为地址。返回一个引用不同于返回一个会导致编译器错误的地址。

关于关于从 'std::ostream&' 类型的临时对象初始化 'std::ostream"类型的非常量引用的 C++ 编译器错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33848564/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com