0
I/O演算子をオーバーロード私は現在、それは不可能私のコードで無限再帰が発生するのを修正するために探しています。C++:問題は
Iは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;
}
このリードを2つに(限り私は無関係の問題を知っている):
- 出力演算子のリードt O
- コード自体が起こっ無限再帰クラスの友人であるにも関わらずアクセス不能 それはクラスのフィールドを主張するように、入力オペレータの実装 をコメントアウトすることなくコンパイルされることができない前記
(「[プロジェクトパス] \ proces.h」の4行目で宣言)重大度コード説明プロジェクトファイルの行の抑制状態 エラー(アクティブ)E0265部材「プロセスへ::名前は」アクセス不可aspdz2 [プロジェクトパス] \プロセスへです。 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 paramがありますが、実装ではそうではありません。 – Whatever
あなたは 'operator >>'フレンド宣言にタイプミスがあります。 2番目の引数から 'const'を削除してください。 –