Re: This fails in g++ but compiles in VC++ 2008. Need Help.
On 07/13/10 12:50 PM, Jag wrote:
On Jul 10, 10:25 pm, Ian Collins<ian-n...@hotmail.com> wrote:
On 07/11/10 04:56 PM, Jag wrote:
template<typename T>
vertex<T>* sgraph<T>::find_a_vertex(const vertex<T>& v) {
typename set<vertex<T> >::iterator itt = _vertices.find(v) ;
if (itt == _vertices.end()) {
return NULL ;
}
vertex<T>& sv = *itt ; //LINE X
vertex<T>* asv =&(sv) ;
return asv ;
}
LINE X
error: invalid initialization of reference of type 'vertex<int>&' from
expression of type 'const vertex<int>'
What exactly is the problem here? Thanks in advance
What the compiler is telling you, a *itt is a const vertex<int>. The
contents of a std::set are immutable (if you were to change a value, the
container ordering would be wrong).
First let me thanks for the answers.
But I am not convinced with this answer:
What the compiler is telling you, a *itt is a const vertex<int>. The
contents of a std::set are immutable (if you were to change a value, the
container ordering would be wrong).
typename set<vertex<T> >::iterator itt = _vertices.find(v) ;
I am inserting an object 'vertex' in the set. The vertex class has
some field (data) that I NEVER change and some fields that I need to
modify. My comparision function in vertex class is follows:
friend bool operator< (const vertex<T>& a, const vertex<T>& b) {
return (a._data< b._data) ;
}
I was under the impression that the 'set' cares about 'constness' of
ONLY compare fields. If that is so, how come vertex class as a 'whole'
is immutable ?
Are you saying that when once we put an object in set, it is NOT
possible to change any field of the object. Please clarify this point.
If this is so, how will I modify the stored object in the set. Should
I delete and add a new one?
To quote Alf's reply (he explained it better than me):
So the std::set elements are 'const'.
And hence std::set<T>::iterator is really a const_iterator.
Deletion and reinsertion is probably the "official" technique. But up
can pull a stunt with smart pointers:
#include <iostream>
#include <set>
#include <tr1/memory.hpp>
struct Ptr : std::tr1::shared_ptr<int>
{
Ptr( int* p) : std::tr1::shared_ptr<int>(p) {}
bool operator<( const Ptr& other ) const { return (**this) < *other; }
};
typedef std::set<Ptr> Set;
int main()
{
Set set;
set.insert( Ptr( new int(1) ) );
set.insert( Ptr( new int(3) ) );
set.insert( Ptr( new int(2) ) );
for( Set::iterator i = set.begin(); i != set.end(); ++i )
std::cout << **i << std::endl;
Set::iterator i = set.begin();
++i;
**i = 42;
for( Set::iterator i = set.begin(); i != set.end(); ++i )
std::cout << **i << std::endl;
}
--
Ian Collins