This error seems to make no sense!
Here's the code (not the entire code, just the relevant parts)
The error is in line 17 In member function `bool Partition<KEY>::iguales(const std::set<KEY, std::less<_Key>, std::allocator<_CharT> >&, const std::set<KEY, std::less<_Key>, std::allocator<_CharT> >&) const':
ERROR: expected `;' before "it" ; `it' undeclared (first use this function) ;
Thanks!. This situation has become pretty common lately, so now I know how to deal with it.
However, when instantiating it as Particion <Estado> im getting many errors.
This is one:
conversion from `std::_List_const_iterator<std::set<Estado, std::less<Estado>, std::allocator<Estado> > >' to non-scalar type `std::_List_iterator<std::set<Estado, std::less<Estado>, std::allocator<Estado> > >' requested
In here
1 2 3
bool ex = false;
for(typename list< set<KEY> >::iterator it = lista_.begin(); it != lista_.end(); it++)
ex = ex||iguales((*it),elem); //bool iguales(const set<KEY>& a ,const set <KEY>& b)
no matching function for call to `std::list<std::set<Estado, std::less<Estado>, std::allocator<Estado> >, std::allocator<std::set<Estado, std::less<Estado>, std::allocator<Estado> > > >::insert(const std::set<Estado, std::less<Estado>, std::allocator<Estado> >&)'
In here
1 2
lista_.insert(elem); //lista_'s type is list< set<Estado> >. list and set are from STL
//lista_ is a private atribute of Particion <Estado>
Since lista_ is const, begin() will return a const_iterator instead of a nonconst iterator. And since 'iterator' and 'const_iterator' are two entirely different types, the compiler is unable to do the converion implicitly.
Note that const_iterator is not const itself -- it means that what it points to is const. You can change the const_iterator, but you can't change what the const_iterator points to.
Since lista_ is const, begin() will return a const_iterator instead of a nonconst iterator. And since 'iterator' and 'const_iterator' are two entirely different types, the compiler is unable to do the converion implicitly
Actually lista_ is not const. lista_ is a private attribute of the class Particion<Estado>. Im wondering how in earth the compiler allows me to do it++ (remember "it" is const_iterator!!)
lista_.push_back(elem) would work too. You only need insert if you're inserting in the middle of the list.
Actually lista_ is not const
That is strange, then. The only way I can make sense of that error is if lista_ is const. Otherwise I don't see a problem.
Unless the function itself is const... in which case 'this' (and by extension lista_) becomes const.
Im wondering how in earth the compiler allows me to do it++ (remember "it" is const!!)
it is not const. It's a const iterator. There's a difference.
It's the same thing as constint* foo vs. int* const foo
1 2 3 4 5 6 7 8 9 10 11
list<int> mylist;
mylist.push_back(5);
const iterator a = mylist.begin();
const_iterator b = mylist.begin();
*a = 10; // okay. We're not changing a, we're changing what it points to
++a; // error, a is const
*b = 10; // error, can't change what const_iterator points to
++b; // okay. b is not const.
Unless the function itself is const... in which case 'this' (and by extension lista_) becomes const.
Right!. The function was const. Now everything makes sense. (plus, the example was fantastic).
I'm still receiving one more error, which is the same on all operator overloads. (excepting operator = ). Im going to show the error on operator ==
in here
1 2 3 4 5
//main.cpp
//pi_vieja and pi are both Particion <Estado>
do{
pi_vieja = pi;
}while(!(pi==pi_vieja));
no match for 'operator==' in 'pi == pi_vieja'
candidates are: bool Particion<KEY>::operator==(const std::list<std::set<KEY, std::less<_Key>, std::allocator<_CharT> >, std::allocator<std::set<KEY, std::less<_Key>, std::allocator<_CharT> > > >&) const [with KEY = Estado]
//This is declared within Particion<KEY>
booloperator == (const list< set<KEY> >& l)const{
bool soniguales = true;
//every element of a is in b
for (typename list< set< KEY > >::const_iterator it=lista_.begin(); it != lista_.end(); it++){
bool elementoencontrado = false;
for (typename set<KEY>::const_iterator jt= l.lista().begin(); jt != l.lista().end(); jt++)
elementoencontrado = (elementoencontrado||((*jt)==(*it)));
soniguales = soniguales&&elementoencontrado;
}
//every element of b is in a
if (soniguales){
for (typename set<KEY>::const_iterator it= l.lista().begin(); it != l.lista().end(); it++){
bool elementoencontrado = false;
for (typename set<KEY>::const_iterator jt= lista_.begin(); jt != lista_.begin(); jt++)
elementoencontrado = (elementoencontrado||((*jt)==(*it)));
soniguales = soniguales&&elementoencontrado;
}
}
return soniguales;
}