Can't figure out this error...
-
At first I tried it too, but I'm not actually iterating through _m_graf_map but _m_graf_map[_ond], which has type std::vector<Ligacao *>. best regards Fratelli
Hi, I don't see it either, but you can try to split the code up to make it easier top spot it. According to the error messages the iterator believes it should use the const iterator instead of the non-const version.
std::vector lstLigacao = this->_m_graf_map[_onde]; std::vector::iterator it; for(it = lstLigacao.begin() ; it != lstLigacao.end() ; it++) { if ( ((*it)->esquerda()->id() == _esq && (*it)->direita()->id() == _dir) || ((*it)->esquerda()->id() == _dir && (*it)->direita()->id() == _esq) ) return true; }
Learn from the mistakes of others, you may not live long enough to make them all yourself.
-
Hi, I don't see it either, but you can try to split the code up to make it easier top spot it. According to the error messages the iterator believes it should use the const iterator instead of the non-const version.
std::vector lstLigacao = this->_m_graf_map[_onde]; std::vector::iterator it; for(it = lstLigacao.begin() ; it != lstLigacao.end() ; it++) { if ( ((*it)->esquerda()->id() == _esq && (*it)->direita()->id() == _dir) || ((*it)->esquerda()->id() == _dir && (*it)->direita()->id() == _esq) ) return true; }
Learn from the mistakes of others, you may not live long enough to make them all yourself.
That works! But I can't afford to copy an entire array like that =/ If that's the case, I rather use vector<>::size_type to iterate through the elements But why does this happen? I mean, lstLigacao and _m_graf_map[_ond] have exactly the same type, right? So why does the iterator work with one, but not the other? :confused: Best regards Fratelli
-
AndreFratelli wrote:
std::vector >
Just a suggestion! Use a typedef to create type for this vector and will make things easier for you...
typedef std::vector<std::vector<Ligacao *> > LigacaoVector2d;
Then create iterators like
LigacaoVector2d::iterator it;
Nibu babu thomas Microsoft MVP for VC++ Code must be written to be read, not by the compiler, but by another human being. Programming Blog: http://nibuthomas.wordpress.com
Yes, I usually do that.. But when dealing with arrays I also define an "iterator" type, like: typedef std::vector<std::vector<Ligacao *> > LigacaoVector2d; typeded LigacaoVector2d::iterator iterator; That way, I can even do: iterator it; Thx anyway ;) =) best regards Fratelli
-
Yes, I usually do that.. But when dealing with arrays I also define an "iterator" type, like: typedef std::vector<std::vector<Ligacao *> > LigacaoVector2d; typeded LigacaoVector2d::iterator iterator; That way, I can even do: iterator it; Thx anyway ;) =) best regards Fratelli
AndreFratelli wrote:
typeded LigacaoVector2d::iterator iterator;
It's better to use it directly so that readers of your code can understand from where this iterator is being used/to which vector this iterator belongs. Isn't it?
Nibu babu thomas Microsoft MVP for VC++ Code must be written to be read, not by the compiler, but by another human being. Programming Blog: http://nibuthomas.wordpress.com
-
That works! But I can't afford to copy an entire array like that =/ If that's the case, I rather use vector<>::size_type to iterate through the elements But why does this happen? I mean, lstLigacao and _m_graf_map[_ond] have exactly the same type, right? So why does the iterator work with one, but not the other? :confused: Best regards Fratelli
Hi, I have tried your code, and it works. I'm using VS2005. But you don't need to copy the array, you could use a reference to it;
std::vector<Ligacao *>& refList = _m_graf_map[_ond];
and use the reference
Learn from the mistakes of others, you may not live long enough to make them all yourself.
-
AndreFratelli wrote:
typeded LigacaoVector2d::iterator iterator;
It's better to use it directly so that readers of your code can understand from where this iterator is being used/to which vector this iterator belongs. Isn't it?
Nibu babu thomas Microsoft MVP for VC++ Code must be written to be read, not by the compiler, but by another human being. Programming Blog: http://nibuthomas.wordpress.com
True =) but, on the other hand, if you have a class with the following definitions:
template <typename T> class MyClass
{
typedef vector<T> my_vector;
typedef my_vector::iterator iterator;
};Then you/others can do something like:
int main()
{
MyClass<type>::iterator it;
return 0;
}Instead of:
int main()
{
MyClass<type>::my_vector::iterator it; // Or even vector<type>::iterator
}Would you agree that it is practical? regards Fratelli
-
True =) but, on the other hand, if you have a class with the following definitions:
template <typename T> class MyClass
{
typedef vector<T> my_vector;
typedef my_vector::iterator iterator;
};Then you/others can do something like:
int main()
{
MyClass<type>::iterator it;
return 0;
}Instead of:
int main()
{
MyClass<type>::my_vector::iterator it; // Or even vector<type>::iterator
}Would you agree that it is practical? regards Fratelli
AndreFratelli wrote:
Would you agree that it is practical?
Yeah, I thought you were letting the iterator typedef float around, if it's in a class then should be fine. :)
Nibu babu thomas Microsoft MVP for VC++ Code must be written to be read, not by the compiler, but by another human being. Programming Blog: http://nibuthomas.wordpress.com
-
Hi, I have tried your code, and it works. I'm using VS2005. But you don't need to copy the array, you could use a reference to it;
std::vector<Ligacao *>& refList = _m_graf_map[_ond];
and use the reference
Learn from the mistakes of others, you may not live long enough to make them all yourself.
I actually get another error =/
e:\projects\netsim\netsim\grafo.cpp(64) : error C2440: 'initializing' : cannot convert from 'const std::vector<_Ty>' to 'std::vector<_Ty> &'
with
[
_Ty=Ligacao *
]
Conversion loses qualifiersThe code is:
std::vector &ref = this->_m_graf_map[_onde];
Regards Fratelli
-
I actually get another error =/
e:\projects\netsim\netsim\grafo.cpp(64) : error C2440: 'initializing' : cannot convert from 'const std::vector<_Ty>' to 'std::vector<_Ty> &'
with
[
_Ty=Ligacao *
]
Conversion loses qualifiersThe code is:
std::vector &ref = this->_m_graf_map[_onde];
Regards Fratelli
Hi, I think i got it. The function where you use this list is probably defined as const. Therefore all calls are made to the constant versions of the vector object. Either change the function not to be const (not good) or use
const std::vector<ligacao*>& ref = lstTT[1]; for (std::vector<ligacao*>::const_iterator it = ref.begin(); it != ref.end(); ++it)
Learn from the mistakes of others, you may not live long enough to make them all yourself.
-
Hi, I think i got it. The function where you use this list is probably defined as const. Therefore all calls are made to the constant versions of the vector object. Either change the function not to be const (not good) or use
const std::vector<ligacao*>& ref = lstTT[1]; for (std::vector<ligacao*>::const_iterator it = ref.begin(); it != ref.end(); ++it)
Learn from the mistakes of others, you may not live long enough to make them all yourself.
It works! :-D You were right, the function is const. Just two things I don't get, though: 1) Why does the iterator must be constant too? 2) Why do you say that making the function not const is not good? I'm asking this because I've seen different opinions in this matter. On one hand, they would argue that making a function const allows the compiler to optimize the function in several ways. On the other hand, I remember reading an article that said that "marking a function const is only a guarantee you're giving to the user that the method will not change the class's attributes" (nor its input, I think...). What do you think? Regards
Fratelli
-
It works! :-D You were right, the function is const. Just two things I don't get, though: 1) Why does the iterator must be constant too? 2) Why do you say that making the function not const is not good? I'm asking this because I've seen different opinions in this matter. On one hand, they would argue that making a function const allows the compiler to optimize the function in several ways. On the other hand, I remember reading an article that said that "marking a function const is only a guarantee you're giving to the user that the method will not change the class's attributes" (nor its input, I think...). What do you think? Regards
Fratelli
AndreFratelli wrote:
- Why do you say that making the function not const is not good?
- When marking a member function as const. You're telling the user of the class that this call to will not change the internal data of the class. This is 'guaranteed'. - You're alsoo telling the compiler to help you the writer too fulfill this design requirement. The compiler will modify for example a member int m_x; to const int m_x;
AndreFratelli wrote:
- Why does the iterator must be constant too?
- The compiler has modified the constantness of this member, meaning that this member cannot be changed. Hence you need to use the const version of the iterator;
Learn from the mistakes of others, you may not live long enough to make them all yourself.
-
AndreFratelli wrote:
- Why do you say that making the function not const is not good?
- When marking a member function as const. You're telling the user of the class that this call to will not change the internal data of the class. This is 'guaranteed'. - You're alsoo telling the compiler to help you the writer too fulfill this design requirement. The compiler will modify for example a member int m_x; to const int m_x;
AndreFratelli wrote:
- Why does the iterator must be constant too?
- The compiler has modified the constantness of this member, meaning that this member cannot be changed. Hence you need to use the const version of the iterator;
Learn from the mistakes of others, you may not live long enough to make them all yourself.
All clear now =D Thanks a lot ! ;) Best regards
Fratelli
-
All clear now =D Thanks a lot ! ;) Best regards
Fratelli