2017-11-17 1 views
0

현재 코드에서 무한 재귀 발생을 수정할 수 없다는 것을 알고 있습니다.C++ : I/O 연산자 오버로드 문제

은 내가 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 (멀리로 나는 관련이 없다는 것을 안다.) 문제 :

  1. 출력 연산자 lead t O
  2. 코드 자체 일어나고 무한 재귀 클래스의 친구이면서 액세스 그것을 클래스의 필드를 주장하는 바와 같이, 입력 조작의 이행 을 주석없이 컴파일 될 수 없다고

심각도 코드 설명 프로젝트 파일 줄 억제 상태 오류 (활성) E0265 구성원 "Proces :: name"("[project path] \ proces.h"의 4 번 줄에서 선언)은 aspdz2 [project path] \ 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; 

} 
그것은 나타납니다
+0

std :: string name; 공개되지 않습니다. 이것이 오류가 발생하는 이유입니다. –

+1

헤더 입력 연산자에 const param이 있지만 구현시 그렇지 않습니다. – Whatever

+1

당신은'operator >>'친구 선언에 오타가 있습니다. 두 번째 인수에서'const'를 제거하십시오. '>>' –

답변

0

심각도 코드 설명 프로젝트 파일 라인 억제 상태 오류 (활성 이유) E0265 구성원 "Proces :: name"("[project path] \ proces.h"의 4 행에 선언 됨)에 액세스 할 수 없습니다 aspdz2 [project path] \ proces.cpp

err 또는 >> 연산자의 선언에 const 키워드가 실수로 포함 된 것입니다. 반면에 재귀는 내가 잊어서 일어났습니다 #include<string>