如何在 c++ 中插入值放
类的一组自定义对象,
我正在尝试创建 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您在参数和方法本身中缺少所有这些
const
。注意:我不确定将
operator<
用于set
中的项目的要求是什么,但您提供的操作符非常弱。You're missing all those
const
s, 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 aset
, but the one you are providing is very weak.@Mat 打败我来回答你的具体问题问题。所以我只是指出一些不惯用的 C++ 问题。您不需要条件来返回布尔值。只需返回
if
语句的值:此外,最好在 C++ 中声明显式转换机制,而不是使用 C 风格的情况:
@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:Also, it's better to state the explicit casting mechanism in C++ rather than use a C-style case:
您需要声明operator==和operator<作为 const 成员函数。这将允许它们在 const 对象上调用,就像存储在集合中的所有对象一样:
正如其他人指出的那样,您的代码示例还存在其他问题:
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 :
AS others have pointed out, there are other issues with your code sample :
正确的签名是:
另外你需要声明
const
函数get_x
和get_y
operator=
有类似的签名,但在这种情况下不是强制性的。The right signature is:
in addition you need to declare
const
the functionget_x
andget_y
The
operator=
has similar signature, but it's not mandatory in this case.其他人已经讨论了 const 问题。我想补充一点,当您声明您的
points_set
时,您可以简单地说:而不是您现在拥有的:
Others have covered the
const
issue. I want to add that when you declare yourpoints_set
you can simply say:instead of what you have now:
您需要将 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.