In my code I am making a graph where I am saving states and I have to, of course, interconnect all of them. I have already repeated the code like 4 times and it always gives me the same error. The code is the following:
graph.hpp
#ifndef _boii_
#define _boii_
#include <set>
#include "state.hpp"
using namespace std;
class grafo{
private:
set<state> my_states;
public:
grafo();
~grafo();
void set_states(int i);
};
#endif
graph.cpp
#include "grafo.hpp"
grafo::grafo(){}
grafo::~grafo(){}
void grafo::set_states(int i){
state a;
a.set_id(i);
my_states.insert(a);
}
and node.hpp
#ifndef _qwerty_
#define _qwerty_
#include <vector>
#include <iostream>
using namespace std;
class state{
private:
int id;
bool acepted;
public:
state();
~state();
int get_id();
bool get_acpt();
void set_id(int i);
void set_acpt(bool i);
int operator < (const state& a);
};
#endif
I will only show the operator of the node object, since it is where the error is being generated, and also that the rest of the libraries are simple sets and gets. the perator < would be:
int state::operator < (const state& a){
if(id < a.id){
return 1;
}
return 0;
}
Finally, the error that I always get is this:
In file included from /usr/include/c++/4.8/bits/stl_tree.h:63:0,
from /usr/include/c++/4.8/set:60,
from grafo.hpp:6,
from grafo.cpp:1:
/usr/include/c++/4.8/bits/stl_function.h: In instantiation of ‘bool std::less<_Tp>::operator()(const _Tp&, const _Tp&) const [with _Tp = state]’:
/usr/include/c++/4.8/bits/stl_tree.h:1324:11: required from ‘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 = state; _Val = state; _KeyOfValue = std::_Identity<state>; _Compare = std::less<state>; _Alloc = std::allocator<state>; std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::key_type = state]’
/usr/include/c++/4.8/bits/stl_tree.h:1377:47: required from ‘std::pair<std::_Rb_tree_iterator<_Val>, bool> std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_M_insert_unique(const _Val&) [with _Key = state; _Val = state; _KeyOfValue = std::_Identity<state>; _Compare = std::less<state>; _Alloc = std::allocator<state>]’
/usr/include/c++/4.8/bits/stl_set.h:463:29: required 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 = state; _Compare = std::less<state>; _Alloc = std::allocator<state>; typename std::_Rb_tree<_Key, _Key, std::_Identity<_Key>, _Compare, typename _Alloc::rebind<_Key>::other>::const_iterator = std::_Rb_tree_const_iterator<state>; std::set<_Key, _Compare, _Alloc>::value_type = state]’
grafo.cpp:12:23: required from here
/usr/include/c++/4.8/bits/stl_function.h:235:20: error: passing ‘const state’ as ‘this’ argument of ‘int state::operator<(const state&)’ discards qualifiers [-fpermissive]
{ return __x < __y; }
^
state.cpp:26:38: error: no ‘int state::operator<(const state&)’ member function declared in class ‘state’
int state::operator < (const state& a){
^
I would really appreciate any help, and thanks in advance.
change, in
nodo.h
by
and in you
nodo.cpp
by
Notice that the only change is to add it
const
to the end of the declaration, right after the closing parenthesis ()
).The error is because the objects in a
std::set< >
are immutable by nature. Internally, its elements are sorted on insert ; if a later operation were to modify them... well, then the order could change, which implies that they would have to be re-ordered.Due to this immutability , the comparison operations when inserting an element are performed on constant types (
const TIPO x
). And you have declared youroperator<( )
as non-constant ( lacks theconst
).Note : as indicated in the comments, the use of
using namespace std;
in the global scope is a bad practice ... you should get used to not using it. It may seem more cumbersome because you have to write more ... but in the long run, you will benefit greatly, and you can save yourself problems that are quite difficult to locate/solve :-)