【问题标题】:comparing == operator for (struct &a, struct &b) vs (const struct &a, const struct &b)比较 (struct &a, struct &b) 与 (const struct &a, const struct &b) 的 == 运算符
【发布时间】:2021-06-15 11:34:43
【问题描述】:

我在头文件中定义了一个名为Point的结构类,如下-

namespace global_planner {
    class GlobalPlanner : public nav_core::BaseGlobalPlanner {
        struct Point {
            __uint32_t x, y; 
            bool operator==(const Point &p1 ) {
                return ((p1.x == x) && (p1.y == y));  
            }
            bool operator<(const Point &p1 ) const {
                return ((p1.x < x) || (p1.x == x && p1.y < y) ) ; 
            }   
        };
    public:
        ///
    private: 
        ////               
    };
    
};

在我的源文件(名为global_planner.cpp)中,我有一个名为generate_straight_path 的函数,定义如下-

bool GlobalPlanner::generate_straight_path(const Point &p1, const Point &p2){        
    if(costmap_ros_->getCost(p1.x, p1.y) == costmap_2d::LETHAL_OBSTACLE) {
        cout << "Point p1 is on obstacle!" << endl;
        return false;
    }
    if(costmap_ros_->getCost(p2.x, p2.y) == costmap_2d::LETHAL_OBSTACLE) {
        cout << "Point p2 is on obstacle!" << endl;
        return false;
    }
    if(p1 == p2) {return 1;}
    if(p1.x == p2.x){}
    if(p1.y == p2.y){}
  }

当我编译 global_planner.cpp 时,我收到以下错误 -

/home/skpro19/catkin_ws/src/my_global_planner/src/global_planner.cpp: In member function ‘bool global_planner::GlobalPlanner::generate_straight_path(const global_planner::GlobalPlanner::Point&, const global_planner::GlobalPlanner::Point&)’:
/home/skpro19/catkin_ws/src/my_global_planner/src/global_planner.cpp:150:11: error: no match for ‘operator==’ (operand types are ‘const global_planner::GlobalPlanner::Point’ and ‘const global_planner::GlobalPlanner::Point’)
     if(p1 == p2) {return 1;}

如果我将generate_straight_path 的定义从generate_straight_path(const Point &amp;p1, const Point &amp;p2) 更改为generate_straight_path(Point &amp;p1, Point &amp;p2),错误就会消失。

为什么会这样?

【问题讨论】:

    标签: c++ struct constants comparator


    【解决方案1】:

    你的相等运算符重载是一个成员函数,但没有标记const

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

    当你把它变成const 成员函数时,它应该可以按预期工作:

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

    通过运算符有点模糊,但是当你这样看时:

    bool GlobalPlanner::generate_straight_path(const Point &p1, const Point &p2){
        // ...
    
        if (p1.operator==(p2)) /* ... */ ;
    }
    

    这是调用成员运算符重载的更详细的方式,您可以看到这个operator==必须是const,因为函数参数const Point&amp; p1本身就是const

    【讨论】:

      【解决方案2】:

      您可以通过删除const 来修复它只是巧合。实际上你需要在这里添加一个:

      bool operator==(const Point &p1 ) const {   return ((p1.x == x) && (p1.y == y));  }   
                                       // ^^^
      

      否则,您只能使用非常量左手操作数调用 operator==

      【讨论】:

        猜你喜欢
        • 2020-07-09
        • 2014-10-19
        • 2011-05-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多