我目前发现在我的代码中无法修复无限递归的发生。
我有以下标题定义了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;
}
这导致两个(据我所知无关)问题:
严重级代码描述项目文件行抑制状态 错误(活动)E0265成员&#34;进程::名称&#34; (在&#34的第4行声明; [项目路径] \ proces.h&#34;)无法访问aspdz2 [项目路径] \ proces.cpp
这是主要功能(&lt;&gt;运算符按预期工作):
#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;
}
答案 0 :(得分:0)
出现原因
严重级代码说明项目文件行抑制状态错误(活动)E0265成员&#34;进程::名称&#34; (在&#34的第4行声明; [项目路径] \ proces.h&#34;)无法访问aspdz2 [项目路径] \ proces.cpp
错误是由于在const
运算符的声明中意外包含了>>
关键字;
另一方面,递归发生是因为我忘了#include<string>