2016-08-05 8 views
0
std::ostream& operator<<(std::ostream&, const Course&); 

void Course::display() { 
    std::cout << std::left << courseCode_ << " | " << std::setw(20) << courseTitle_ << " | " << std::right 
     << std::setw(6) << credits_ << " | " << std::setw(4) << studyLoad_ << " | "; 
} 

std::ostream& operator<<(std::ostream& os, const Course& a) { 
    a.display(); 
    return os; 
} 

a.display() 이하의 ostream 연산자를 구현할 때 문제가 발생합니다. 문제가있는 곳이 보이지 않습니다. 동일한 구현으로 작동하는 다른 코드가 있습니다.개체에 멤버 함수와 호환되지 않는 형식 한정자가 있습니다. C++

오류 메시지 :

목적은 멤버 함수 "SICT :: 과정 :: 디스플레이"개체 형식과 호환되지 않는 한정자를 입력있다가 const를 SICT이다 :: 코스

+0

가능 중복 [링크] (http://stackoverflow.com/questions/24677032/object-has-type-qualifiers-that-are-not-compatible-with-the-member-function) –

+2

'Course :: display' 함수는 왜'std :: cout'에 쓰기 위해 하드 코드되어 있습니까? 파일에 쓰기를 원한다면 (이것은'operator <<'오버로드로 가능할 것입니다)? –

답변

4

에서 operator<<(), a이 const로 선언되어 있기 때문에 a.display();이 실패합니다. 거기에 비 const 멤버 함수를 호출 할 수 없습니다.

Course::display()은 const 멤버 함수로 선언해야합니다. 아무 것도 수정하지 않아야합니다.

void Course::display() const { 
         ~~~~~ 
    ... 
}