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

C++ compiler error regarding initialization of a non-const reference of type 'std::ostream&' from a temporary of type 'std::ostream"

我一直在尝试创建一个程序来为一组已定义的进程实现实时调度算法。使用 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 的类型不起作用,更改 return 类型似乎也不起作用,我有点无计可施了。我在这里找到了引用类似内容的答案,但 none 的解决方案似乎解决了我的问题。如果能帮助解决我的错误,我们将不胜感激。

改变

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

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

正如在对您的 post 的第一条评论中所指出的,不要这样做

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*'

因此,当函数 return 是一个引用时,编译器意识到您正在尝试 return 一个指针,并且它会向您抛出一个错误。