2011-08-10 19 views
2

オーバーロードされた< <演算子関数を実装しようとするといくつかの問題が発生していましたが、これは自分のクラスの1つであるstd :: list 。クラスは次のようになります。<<演算子をオーバーロードして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 Sを返すconst修飾であり、const_iterator Sを返します。このようにして、constではないコンテナを繰り返し処理することができ、その中の要素を読み込んで変更することもできますが、constコンテナに対しては読み取り専用アクセスで反復処理することもできます。

+0

私はあなたがしたように速く返答したか分かりませんが、あなたは完全に正しいです。これはfi私の問題に対処しました。私はこれがstdライブラリの理解の私の不足を強調していると思うが、私はひどくひどくなるだろう...ありがとう! –

6

あるいは、

あなたにstd::copyを使用することができます:operator<<の署名がこのであることを確認してください

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

std::ostream & operator <<(std::ostream &out, const Point &pt); 
              //^^^^^ note this 
関連する問題