1
coord front_creature = gs->creatures.front()->get_coord();     //get location of creature closest to base
 coord end = gs->map->get_end();                      //get location of base
    if (front_creature == end) {           //if creature location == base location, then game over
      exit_run_mode();
  }

get_coord および get_end 関数は const coord & を返し、上記のコードにはエラーはありません。しかし、次のコードに置き換えると、Visual Studio は「これらのオペランドに一致する演算子 '==' はありません」と表示します。個々の関数を括弧で囲んでみましたが、うまくいきませんでした。

if (gs->creatures.front()->get_coord() == gs->map->get_end()) {      
  exit_run_mode();
}
4

1 に答える 1

3

operator==非メンバー関数の場合、次のようにする必要があると思います。

bool operator==(const coord& a, const coord b&);

または、coord のメンバー関数の場合は、次のようになります。

bool operator==(const coord& rhs) const;

あなたが何かをするとき

coord front_creature = gs->creatures.front()->get_coord();

式によって返されたを使用const coord&しており、それを使用して新しい非 const を初期化していますcoord。それがおそらく最初の例が機能する理由です。non-const を比較しているためですcoords

于 2012-04-26T05:41:38.723 に答える