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&表达式返回的值,并使用它来初始化一个新的非常量coord。这可能就是您的第一个示例有效的原因。因为您正在比较 non-const coords

于 2012-04-26T05:41:38.723 回答