如何在 c++ 中插入值放

发布于 2024-12-09 02:18:03 字数 3074 浏览 1 评论 0原文

类的一组自定义对象,

我正在尝试创建 Point point.h

#ifndef POINT_H
#define POINT_H

class Point
{
      float x_coordinate,y_coordinate;
      public:
             Point(float x, float y):x_coordinate(x),y_coordinate(y)
             {}

             float get_x()
             {
                   return x_coordinate;
             }

             float get_y()
             {
                   return y_coordinate;
             }

             bool operator==(Point rhs)
             {
                  if( ((int)x_coordinate == (int)rhs.get_x()) && ((int)y_coordinate == (int)rhs.get_y()) )
                      return true;
                  else return false;
             }

             bool operator<(Point rhs)
             {
                  if((int)x_coordinate < (int)rhs.get_x())
                      return true;
                  else return false;
             }
};

#endif

我刚刚开始编写驱动程序

#include<iostream>
#include<set>
#include "point.h"
using namespace std;



int main()
{
    Point p1(-10,-10),p2(-10,10),p3(10,10),p4(10,-10);
    set<Point> points_set = set<Point>();
    points_set.insert(p1);

    return 0;
}

,但在编译过程中出现此错误,我已经重载了比较和相等运算符,我还必须做什么让它发挥作用吗?

In file included from /usr/include/c++/4.6/string:50:0,
                 from /usr/include/c++/4.6/bits/locale_classes.h:42,
                 from /usr/include/c++/4.6/bits/ios_base.h:43,
                 from /usr/include/c++/4.6/ios:43,
                 from /usr/include/c++/4.6/ostream:40,
                 from /usr/include/c++/4.6/iostream:40,
                 from driver.cpp:1:
/usr/include/c++/4.6/bits/stl_function.h: In member function ‘bool std::less<_Tp>::operator()(const _Tp&, const _Tp&) const [with _Tp = Point]’:
/usr/include/c++/4.6/bits/stl_tree.h:1267:4:   instantiated from ‘std::pair<std::_Rb_tree_iterator<_Val>, bool> std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_M_insert_unique(const _Val&) [with _Key = Point, _Val = Point, _KeyOfValue = std::_Identity<Point>, _Compare = std::less<Point>, _Alloc = std::allocator<Point>]’
/usr/include/c++/4.6/bits/stl_set.h:410:29:   instantiated from ‘std::pair<typename std::_Rb_tree<_Key, _Key, std::_Identity<_Key>, _Compare, typename _Alloc::rebind<_Key>::other>::const_iterator, bool> std::set<_Key, _Compare, _Alloc>::insert(const value_type&) [with _Key = Point, _Compare = std::less<Point>, _Alloc = std::allocator<Point>, typename std::_Rb_tree<_Key, _Key, std::_Identity<_Key>, _Compare, typename _Alloc::rebind<_Key>::other>::const_iterator = std::_Rb_tree_const_iterator<Point>, std::set<_Key, _Compare, _Alloc>::value_type = Point]’
driver.cpp:12:25:   instantiated from here
/usr/include/c++/4.6/bits/stl_function.h:236:22: error: passing ‘const Point’ as ‘this’ argument of ‘bool Point::operator<(Point)’ discards qualifiers [-fpermissive]

I'm trying to create a set of custom objects of the class Point

point.h

#ifndef POINT_H
#define POINT_H

class Point
{
      float x_coordinate,y_coordinate;
      public:
             Point(float x, float y):x_coordinate(x),y_coordinate(y)
             {}

             float get_x()
             {
                   return x_coordinate;
             }

             float get_y()
             {
                   return y_coordinate;
             }

             bool operator==(Point rhs)
             {
                  if( ((int)x_coordinate == (int)rhs.get_x()) && ((int)y_coordinate == (int)rhs.get_y()) )
                      return true;
                  else return false;
             }

             bool operator<(Point rhs)
             {
                  if((int)x_coordinate < (int)rhs.get_x())
                      return true;
                  else return false;
             }
};

#endif

I've just started to write the driver

#include<iostream>
#include<set>
#include "point.h"
using namespace std;



int main()
{
    Point p1(-10,-10),p2(-10,10),p3(10,10),p4(10,-10);
    set<Point> points_set = set<Point>();
    points_set.insert(p1);

    return 0;
}

but I get this error during compilation, I have overloaded the comparison and equality operator, what else must I do to get it to work?

In file included from /usr/include/c++/4.6/string:50:0,
                 from /usr/include/c++/4.6/bits/locale_classes.h:42,
                 from /usr/include/c++/4.6/bits/ios_base.h:43,
                 from /usr/include/c++/4.6/ios:43,
                 from /usr/include/c++/4.6/ostream:40,
                 from /usr/include/c++/4.6/iostream:40,
                 from driver.cpp:1:
/usr/include/c++/4.6/bits/stl_function.h: In member function ‘bool std::less<_Tp>::operator()(const _Tp&, const _Tp&) const [with _Tp = Point]’:
/usr/include/c++/4.6/bits/stl_tree.h:1267:4:   instantiated from ‘std::pair<std::_Rb_tree_iterator<_Val>, bool> std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_M_insert_unique(const _Val&) [with _Key = Point, _Val = Point, _KeyOfValue = std::_Identity<Point>, _Compare = std::less<Point>, _Alloc = std::allocator<Point>]’
/usr/include/c++/4.6/bits/stl_set.h:410:29:   instantiated from ‘std::pair<typename std::_Rb_tree<_Key, _Key, std::_Identity<_Key>, _Compare, typename _Alloc::rebind<_Key>::other>::const_iterator, bool> std::set<_Key, _Compare, _Alloc>::insert(const value_type&) [with _Key = Point, _Compare = std::less<Point>, _Alloc = std::allocator<Point>, typename std::_Rb_tree<_Key, _Key, std::_Identity<_Key>, _Compare, typename _Alloc::rebind<_Key>::other>::const_iterator = std::_Rb_tree_const_iterator<Point>, std::set<_Key, _Compare, _Alloc>::value_type = Point]’
driver.cpp:12:25:   instantiated from here
/usr/include/c++/4.6/bits/stl_function.h:236:22: error: passing ‘const Point’ as ‘this’ argument of ‘bool Point::operator<(Point)’ discards qualifiers [-fpermissive]

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

记忆里有你的影子 2024-12-16 02:18:03
float get_x() const { ... }
float get_y() const { ... }
bool operator==(Point const& rhs) const { ... }
bool operator<(Point const& rhs) const { ... }

您在参数和方法本身中缺少所有这些 const

注意:我不确定将 operator< 用于 set 中的项目的要求是什么,但您提供的操作符非常弱。

float get_x() const { ... }
float get_y() const { ... }
bool operator==(Point const& rhs) const { ... }
bool operator<(Point const& rhs) const { ... }

You're missing all those consts, in the arguments and for the methods themselves.

Note: I'm not sure what the requirements are for operator< to be used for items in a set, but the one you are providing is very weak.

错爱 2024-12-16 02:18:03

@Mat 打败我来回答你的具体问题问题。所以我只是指出一些不惯用的 C++ 问题。您不需要条件来返回布尔值。只需返回 if 语句的值:

bool operator==(const Point& rhs) const
{
    return ((int)x_coordinate == (int)rhs.get_x()) && ((int)y_coordinate == (int)rhs.get_y());
}

bool operator<(const Point& rhs) const
{
    return (int)x_coordinate < (int)rhs.get_x();
}

此外,最好在 C++ 中声明显式转换机制,而不是使用 C 风格的情况:

    return static_cast<int>(x_coordinate) < static_cast<int>(rhs.get_x());

@Mat beat me to it to answer your specific question. So I'll just point-out some issue with your un-idiomatic C++. You don't need a conditional to return a boolean. Just return the value of what would have been your if statement:

bool operator==(const Point& rhs) const
{
    return ((int)x_coordinate == (int)rhs.get_x()) && ((int)y_coordinate == (int)rhs.get_y());
}

bool operator<(const Point& rhs) const
{
    return (int)x_coordinate < (int)rhs.get_x();
}

Also, it's better to state the explicit casting mechanism in C++ rather than use a C-style case:

    return static_cast<int>(x_coordinate) < static_cast<int>(rhs.get_x());
鸠魁 2024-12-16 02:18:03

您需要声明operator==和operator<作为 const 成员函数。这将允许它们在 const 对象上调用,就像存储在集合中的所有对象一样:

bool operator<(Point rhs) const

正如其他人指出的那样,您的代码示例还存在其他问题:

  • get_x 和 get_y 也应该是 const 方法,
  • 您的运算符<的实现也应该是 const 方法。相比之下,它太宽松了。 std::set 在执行插入和查找时使用“等价”的概念。事实上,几个不同的对象可能无法与operator<进行一致的比较。 (即它不是“弱排序函数”)肯定会与其他 std::set 操作发生冲突。特别是,在您的代码示例中,该集合无法区分 (-10, -10) 和 (-10, 10)。您至少应该比较 x 和 y 坐标的点,并使用浮点比较而不是整数比较

You need to declare operator== and operator< as const member functions. This will allow them to be invoked on const objects, like all objects stored in a set are :

bool operator<(Point rhs) const

AS others have pointed out, there are other issues with your code sample :

  • get_x and get_y should be const methods too
  • your implementation of operator< is too laxist in its comparison. std::set uses the notion of "equivalence" when performing insertions and lookups. The fact that several different objects may not compare consistently with operator< (i.e. it is not a "weak ordering function") will definitely screw with other std::set operations. In particular, in your code sample, the set is not able to distinguish between (-10, -10) and (-10, 10). You should at least compare points for both the x and y coordinates, and use floating-point comparison instead of integer comparison
吐个泡泡 2024-12-16 02:18:03

正确的签名是:

bool operator<(const Point & rhs) const

另外你需要声明const函数get_xget_y

operator=有类似的签名,但在这种情况下不是强制性的。

The right signature is:

bool operator<(const Point & rhs) const

in addition you need to declare const the function get_x and get_y

The operator= has similar signature, but it's not mandatory in this case.

雪落纷纷 2024-12-16 02:18:03

其他人已经讨论了 const 问题。我想补充一点,当您声明您的 points_set 时,您可以简单地说:

set<Point> points_set;

而不是您现在拥有的:

set<Point> points_set = set<Point>();

Others have covered the const issue. I want to add that when you declare your points_set you can simply say:

set<Point> points_set;

instead of what you have now:

set<Point> points_set = set<Point>();
何止钟意 2024-12-16 02:18:03

您需要将 Point 作为 const& 传递;给两个运营商。运算符本身也应该是 const。

我还注意到运算符<不是比较 y 坐标,这是故意的吗?考虑 (0/7) < (0/5) 为假且 (0/5) < (0/7) 也将为假。

You need to pass Point as const& to both operators. The operators themselves should really also be const.

I also notice operator< isn't comparing the y coordinate, is that intentional? Consider that (0/7) < (0/5) will be false and (0/5) < (0/7) will also be false.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文