gpt4 book ai didi

c++ - C++ 中运算符 < 重载的问题

转载 作者:行者123 更新时间:2023-11-28 02:35:12 26 4
gpt4 key购买 nike

我正在学习运算符重载,并试图将自定义类的两个顶点添加到一个集合中。这会导致奇怪的错误和我对 < 的尝试重载没有工作。

谁能解释一下哪里出了问题?

我的 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

最佳答案

operator<函数需要是 const成员函数。改成

bool operator < (const Vertex &o) const;

关于c++ - C++ 中运算符 < 重载的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27738637/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com