2011-08-10 13 views
2

오버로드 된 < < 연산자 함수를 구현하는 데 문제가 발생하여 내 클래스 중 하나의 멤버 인 std :: list를 인쇄 할 수 있습니다. . 클래스는 다음과 같습니다 : 나는에 관심이<< 연산자를 오버로드하여 표준 출력 :

class NURBScurve { 
    vector<double> knotVector; 
    int curveOrder; 
    list<Point> points; 
public: 
    /* some member functions */ 
    friend ostream& operator<< (ostream& out, const NURBScurve& curve); 
}; 

의 핵심 멤버 변수는 "포인트"의 목록입니다 - 이것이 내가 관련된 멤버 함수와 함께 지점의 좌표를 저장하는 만든 또 다른 클래스입니다. 나는 시도하고 오버로드 < < 운영자 기능을 구현할 때 :

ostream& operator<<(ostream &out, const NURBScurve &curve) 
{ 
out << "Control points: " << endl; 
list<Point>::iterator it; 
for (it = curve.points.begin(); it != curve.points.end(); it++) 
    out << *it; 
out << endl; 
return out; 
} 

내가 문제를 얻기 시작합니다. 내가 조금 여기 난처한 상황에 빠진거야

no match for ‘operator=’ in ‘it = curve->NURBScurve::points. std::list<_Tp, _Alloc>::begin [with _Tp = Point, _Alloc = std::allocator<Point>]()’ 
/usr/include/c++/4.2.1/bits/stl_list.h:113: note: candidates are: std::_List_iterator<Point>& std::_List_iterator<Point>::operator=(const std::_List_iterator<Point>&) 

,하지만 난 그게 내가 사용하고,리스트 반복자 함께 할 수있는 뭔가가 생각 : 오류 : 특히, 나는 다음과 같은 오류가 발생합니다. 나는 또한 curve.points.begin()의 표기법에 너무 자신이 없다.

누군가가 문제에 대해 밝힐 수 있다면 고맙겠습니다. 나는 그 문제를 너무 오래 꼼짝 않고 바라보고 있었다.

+0

당신은 [꽤 프린터]를 줄 수 (HTTP ://stackoverflow.com/questions/4850473/pretty-print-c-stl-containers) 시도 :-) –

답변

9

curve 그렇게 curve.points는 const를-자격 및 curve.points.begin()std::list<Point>::const_iterator 아닌 std::list<Point>::iterator 반환 const를-자격이 부여됩니다.

용기와 두 begin()end() 부재 기능을 가지고 한 쌍 CONST 자격 멤버 함수 아니며 iterator들 돌아가 다른 쌍 CONST 정규화하고 const_iterator S를 반환한다. 이 방법을 사용하면 const가 아닌 컨테이너를 반복 할 수 있으며 요소를 읽고 수정하지만 읽기 전용 액세스로 const 컨테이너를 반복 할 수도 있습니다.

std::copy(points.begin(), points.end(), 
         std::ostream_iterator<Point>(outStream, "\n")); 

operator<<의 서명이 있는지 확인 :

당신은으로 std::copy을 사용할 수

+0

나는 당신이 한 것처럼 빨리 대답 한 방법을 모르지만, 당신은 완전히 옳았습니다. 그리고 이것은 fi를 가지고 있습니다. 내 문제. 이게 std 라이브러리에 대한 이해가 부족하다는 것을 강조한 것 같지만, 계속 크래킹 할 것입니다 ... 고마워요! –

6

또는

std::ostream & operator <<(std::ostream &out, const Point &pt); 
              //^^^^^ note this 
관련 문제