关于类型' std :: ostream&'的非const引用的初始化的C ++编译器错误来自临时类型' std :: ostream"

时间:2015-11-21 21:05:52

标签: c++ ostream

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

RTSprocess.h:在函数' std :: ostream&运算符<<(std :: ostream&,const rtsProcess&)': RTSprocess.h84:错误:类型' std :: ostream&'的非const引用的无效初始化来自临时类型' 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的类型不起作用,更改返回类型似乎不起作用,我&#39有点像我的智慧结束。我在这里找到了引用类似内容的答案,但没有一个解决方案可以解决我的问题。任何帮助解决我的错误都将非常感激。

2 个答案:

答案 0 :(得分: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;
};

运算符&被称为地址。返回引用不同于返回产生编译器错误的地址。

答案 1 :(得分:3)

正如您在帖子的第一条评论中指出的那样,不要做

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

只做

return os;

每当你做

&x

其中x是某个变量,你得到一个指向该变量的临时指针。因此出现错误消息

  

RTSprocess.h84:错误:类型的非const引用的无效初始化&#39; std :: ostream&amp;&#39;来自临时类型&#39; std :: ostream *&#39;

因此编译器意识到当函数返回引用时你试图返回一个指针,它会引发错误。