【问题标题】:C++: Issue overloading I/O operatorsC++:问题重载 I/O 运算符
【发布时间】:2018-05-01 12:06:27
【问题描述】:

我目前发现无法修复我的代码中出现的无限递归。

我有以下定义 Proces 类的标头:

#pragma once
#include <iostream>
class Proces {
    std::string name;
    int maxWaitTime;
    int timeToFinish;
    int timeWaited;
    int timeProcessed;

public:
    Proces(std::string n, int mwt = 1, int ttf = 1) :name(n), maxWaitTime(mwt), timeToFinish(ttf), timeWaited(0), timeProcessed(0) {}
    bool process(int a = 1) { timeProcessed += a; return isComplete(); }
    bool isComplete() { return timeProcessed >= timeToFinish; }
    bool wait(int a = 1) { timeWaited += a;return maxWaitReached(); }
    bool maxWaitReached() { return maxWaitTime <= timeWaited; }
    friend bool operator<(const Proces& a, const Proces& b);
    friend bool operator>(const Proces& a, const Proces& b);
    friend std::ostream &operator<<(std::ostream &output, const Proces& a);
    friend std::istream &operator>>(std::istream &input, const Proces& a);
};

然后,对于运算符的实现,我有:

 #include "proces.h"

    bool operator<(const Proces & a, const Proces & b)
    {
        if (a.timeWaited != b.timeWaited)
            return a.timeWaited < b.timeWaited;
        else
            return a.maxWaitTime < b.maxWaitTime;
    }

    bool operator>(const Proces & a, const Proces & b)
    {
        return ! (a < b);
    }

    std::ostream & operator<<(std::ostream & output, const Proces & a)
    {
        output << a.naziv << " MWT:" << a.maxWaitTime << " TTC:" << a.timeToFinish << " WT:" << a.timeWaited << " TP:" << a.timeProcessed;
        return output;
    }

    std::istream & operator>>(std::istream & input,Proces & a)
    {
        input >> a.name >> a.maxWaitTime >> a.timeToFinish;
        a.timeWaited = 0;
        a.timeProcessed = 0;
        return input;
    }

这会导致两个(据我所知不相关的)问题:

  1. 使用输出运算符会导致上述无限递归发生
  2. 如果不注释掉实现,代码本身无法编译 输入运算符,因为它声称类的字段不可访问 尽管是班上的朋友

严重性代码描述项目文件行抑制状态 错误(活动)E0265 成员“Proces::name”(在“[项目路径]\proces.h”的第 4 行声明)无法访问 aspdz2 [项目路径]\proces.cpp

这是主要功能( 运算符按预期工作):

#include "proces.h"

int main() {
    Proces a{ "Glorious prces",1,2 };
    Proces b{ "Glorious prces2",2,2 };

    if (a < b)std::cout << "A is lesser" << std::endl;
    else std::cout << "B is lesser" << std::endl;

    if (a > b)std::cout << "A is greater" << std::endl;
    else std::cout << "B is greater" << std::endl;
    b.wait(-1);
    if (a < b)std::cout << "A is lesser" << std::endl;
    else std::cout << "B is lesser" << std::endl;

    //Infinite recursion happens here:
    std::cout << b << std::endl;

}

【问题讨论】:

  • std::字符串名称;它不公开。这就是您收到错误的原因。
  • 在头部输入操作符有 const 参数,在实现中没有。
  • 您在operator&gt;&gt; 朋友声明中有错字。从第二个参数中删除const
  • 是的,const 的事情是错误之一,但在修复它之后,我得到“严重代码描述项目文件行抑制状态错误 C2678 二进制 '>>':没有找到左转的运算符- 'std::istream' 类型的手动操作数(或没有可接受的转换)[项目路径]\proces.cpp "

标签: c++ operator-overloading operators ostream istream


【解决方案1】:

看来是这个原因

严重性代码描述项目文件行抑制状态错误(活动)E0265 成员“Proces::name”(在“[项目路径]\proces.h”的第 4 行声明)无法访问 aspdz2 [项目路径]\proces。 cpp

错误是由于在&gt;&gt; 运算符的声明中意外包含了const 关键字; 另一方面,递归正在发生,因为我忘记了#include&lt;string&gt;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-03-17
    • 2014-04-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多