我只是在学习运算符重载,并且正在尝试将自定义类的两个顶点添加到集合中。这会导致奇怪的错误,我<
重载的尝试不起作用。
有人可以解释什么是错的吗?
我的Vertex
课程:
class Vertex{
public:
int i, j;
set<Vertex> adj; //adjacent vertices
Vertex(){
i = j = -1;
}
~Vertex(){
adj.clear();
}
//end constructors and destructors
void setPos(int row, int col){
i = row;
j = col;
}//end setPos()
/** must overload for set<Vertex> to function */
bool operator < (const Vertex &o){
if(i < o.i)
return true;
if(i > o.i)
return false;
return j < o.j;
}
};//END class Vertex
但是在main中调用此函数会导致终端中的奇怪输出并出现错误:
/** connect v1 and v2 such that they are adjacent */
void addEdge(Vertex v1, Vertex v2){
v1.adj.insert(v2);
v2.adj.insert(v1);
}//END addEdge()
错误:
In file included from c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\string:48:0,
from c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\locale_cla
sses.h:40,
from c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\ios_base.h
:41,
from c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\ios:42,
from c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\ostream:38,
from c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\iostream:39,
from FileMaze.cc:2:
c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\stl_function.h: In instantiation
of 'bool std::less<_Tp>::operator()(const _Tp&, const _Tp&) const [with _Tp = V
ertex]':
c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\stl_tree.h:1321:11: required f
rom 'std::pair<std::_Rb_tree_node_base*, std::_Rb_tree_node_base*> std::_Rb_tree
<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_M_get_insert_unique_pos(const key_
type&) [with _Key = Vertex; _Val = Vertex; _KeyOfValue = std::_Identity<Vertex>;
_Compare = std::less<Vertex>; _Alloc = std::allocator<Vertex>; std::_Rb_tree<_K
ey, _Val, _KeyOfValue, _Compare, _Alloc>::key_type = Vertex]'
c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\stl_tree.h:1374:47: required f
rom 'std::pair<std::_Rb_tree_iterator<_Val>, bool> std::_Rb_tree<_Key, _Val, _Ke
yOfValue, _Compare, _Alloc>::_M_insert_unique(const _Val&) [with _Key = Vertex;
_Val = Vertex; _KeyOfValue = std::_Identity<Vertex>; _Compare = std::less<Vertex
>; _Alloc = std::allocator<Vertex>]'
c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\stl_set.h:463:29: required fro
m 'std::pair<typename std::_Rb_tree<_Key, _Key, std::_Identity<_Key>, _Compare,
typename _Alloc::rebind<_Key>::other>::const_iterator, bool> std::set<_Key, _Com
pare, _Alloc>::insert(const value_type&) [with _Key = Vertex; _Compare = std::le
ss<Vertex>; _Alloc = std::allocator<Vertex>; typename std::_Rb_tree<_Key, _Key,
std::_Identity<_Key>, _Compare, typename _Alloc::rebind<_Key>::other>::const_ite
rator = std::_Rb_tree_const_iterator<Vertex>; std::set<_Key, _Compare, _Alloc>::
value_type = Vertex]'
FileMaze.cc:47:18: required from here
c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\stl_function.h:235:20: error: pa
ssing 'const Vertex' as 'this' argument of 'bool Vertex::operator<(const Vertex&
)' discards qualifiers [-fpermissive]
{ return __x < __y; }
^
make:*** [FileMaze.o]错误1
答案 0 :(得分:2)
operator<
函数需要是const
成员函数。将其更改为
bool operator < (const Vertex &o) const;
答案 1 :(得分:0)
我建议您查看this page of c++ FAQ。
事实上,当您重载Less operator
之类的功能时,您不会更改对象,因此预计会重载为const member function
:
bool operator < (const Vertex &o) const {
^~~~~
}