操作员<C++过载问题
Trouble with operator < overloading in C++
我只是在学习运算符重载,并尝试将自定义类的两个顶点添加到集合中。 这会导致奇怪的错误,我尝试<
重载没有奏效。
有人可以解释出了什么问题吗?
我的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:mingwlibgccmingw324.8.1includec++string:48:0,
from c:mingwlibgccmingw324.8.1includec++bitslocale_cla
sses.h:40,
from c:mingwlibgccmingw324.8.1includec++bitsios_base.h
:41,
from c:mingwlibgccmingw324.8.1includec++ios:42,
from c:mingwlibgccmingw324.8.1includec++ostream:38,
from c:mingwlibgccmingw324.8.1includec++iostream:39,
from FileMaze.cc:2:
c:mingwlibgccmingw324.8.1includec++bitsstl_function.h: In instantiation
of 'bool std::less<_Tp>::operator()(const _Tp&, const _Tp&) const [with _Tp = V
ertex]':
c:mingwlibgccmingw324.8.1includec++bitsstl_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:mingwlibgccmingw324.8.1includec++bitsstl_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:mingwlibgccmingw324.8.1includec++bitsstl_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:mingwlibgccmingw324.8.1includec++bitsstl_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 ++常见问题解答。
事实上,当你重载像Less operator
这样的函数时,你不会改变对象,所以预计重载const member function
:
bool operator < (const Vertex &o) const {
^~~~~
}
相关文章:
- 警告处理为错误这里有什么问题
- 最小硬币更换问题(自上而下方法)
- 为"adjacent"变量赋值时出现问题
- 我的神经网络不起作用 [XOR 问题]
- 在Ubuntu 16.04上安装Cilk时出现问题
- C++我的数学有什么问题,为什么我的代码不能正确循环
- 编译包含字符串的代码时遇到问题
- Project Euler问题4的错误解决方案
- 问题:什么是QAbstractItemView::NoEditTriggers的反面
- 在编译C++代码(具有dlib和opencv)到WASM时面临问题
- 在进程中对同一管道进行读取和写入时C++管道出现问题
- 静态数据成员的问题-修复链接错误会导致编译器错误
- C++ 雷神库 - 使用资源加载器类时出现问题(不命名类型)
- 一个关于在C++中重载布尔运算符的问题
- 首要问题的答案让值班员搞错了
- setlocale的C++土耳其字符串问题
- 如何重构类层次结构以避免菱形问题
- 基于boost的程序的静态链接——zlib问题
- C++格式化输出问题
- 使用mongocxx驱动程序时包含头文件问题