2017-01-18 2 views
0

안녕하세요 everonye도이 질문에 대한 답을 많이 찾았습니다. 가는 것은 모든 것을 시도하는 그것을 고칠 수 없었다.오류 : 'std :: ostream {aka std :: basic_ostream <char>}'lvalue to 'std :: basic_ostream <char> &&'

그래서 제 질문은 내가 내가

여기
error: cannot bind ‘std::ostream {aka std::basic_ostream<char>}’ lvalue to ‘std::basic_ostream<char>&&’ 

내 방법의 구현 클래스의 모습입니다 오류를 다음 얻을 인라인 방법으로 운영자 < <을 구현하려고 할 때 매트릭스라는 클래스를 가지고있다

ostream& operator<<(ostream& out) 
{ 

    for (int i = 0; i < this->getHeight(); ++i) { 
     for (int j = 0; j < this->getWidth(); ++j) { 

      out << this->(i, j) << "\t"; 
     } 
     out<< "\n"; 
    } 

    return out; 
} 
나는이

template <typename U> 
ostream& operator<<(ostream& out, const Matrix<U>& c) 
{ 

    for (int i = 0; i < c.getHeight(); ++i) { 
     for (int j = 0; j < c.getWidth(); ++j) { 

      out << c(i, j) << "\t"; 
     } 
     out << "\n"; 
    } 

    return out; 
} 
같은 기능으로 구현

그것은 작동 :( 사람이 내가 잘못 여기

+0

'this -> (i, j)'가 실제로 컴파일됩니까 ?? – Quentin

답변

0

std::ostream& Matrix<U>::operator <<(std::ostream&)하고있어 걸 설명 할 수 하지 당신이 원하는 (여러 가지 이유로)입니다 동등한 자유 함수 서명 std::ostream& operator <<(Matrix<U>&, ostream&)을 가지고 있습니다.

첫 번째 매개 변수는 항상 스트림이어야하므로 operator << 또는 operator >>은 UDT의 멤버 함수로 스트리밍을 구현할 수 없습니다. 당신은 서명 할 필요가있는 경우에 연산자 friend, 만들 수 있습니다 : (. 또, 스트림이 작동 코드에서와 같이, 먼저 참고)

template <typename U> 
friend std::ostream& operator <<(std::ostream&, Matrix const&); 

귀하의 템플릿 operator << 모습을 잘 됐네. 네가 그 반대 의견을 갖고 있니?

관련 문제