3

私はこのようなクラスを持っています:

class Point
{
public:
   int x;
   int y;

   bool operator==( Point& other )
   {
       return (x == other.x) && (y = other.y);
   }
};

次に、ポイントのベクトルを持つ初歩的な Sprite クラスと、以下に示していない他の多くのメンバー/メソッドがあります。

class Sprite
{
   std::vector<Point> points;
};

Point クラスの operator== を使用して、別の Sprite オブジェクトのベクトル内の他の点と衝突する Sprite オブジェクトのベクトル内の任意の Point をどのように見つけますか? 次のようなことを試しましたが、うまくいきません。Size()はポイントのベクトル内のポイントの総数を返し、Points()はポイントのベクトルを返します。

bool Sprite::CollidesWith( Sprite* other )
{
  for ( int ixThis = 0; ixThis < Size(); ixThis++ ) {
    for ( int ixOther = 0; ixOther < other->Size(); ixOther++ ) {
      if ( points->at(ixThis) == other->Points()->at(ixOther) )
        return true;
    }
  }

  return false;
}

何か案は?

4

1 に答える 1

2

あなたoperator==は間違っています。

   bool operator==( Point& other )
   {
       return (x == other.x) && (y == other.y);
   }
于 2012-12-09T00:16:34.277 に答える