Simple Question I am sure !!!!! 
Author Message
 Simple Question I am sure !!!!!

Hi,

I am using a STL map for the first time. I have managed to store a pointer
to my class in the list. But I now need to get it out of the list.

Looking at the sample on MSDN I think I am quite close however - the
documentation is FAR from clear.

 CclsHostConnection* pHostConnection;
 HOSTMAP::iterator hostIterator;

 for (hostIterator = m_lstHostConnections.begin(); hostIterator !=
m_lstHostConnections.end(); ++hostIterator)
 {
      pHostConnection = (CclsHostConnection*) hostIterator;
      pHostConnection->UpdateStatus();
 }

The case from the iterator back to the correct type failes with the
following error message

error C2440: 'type cast' : cannot convert from 'class std::_Tree<int,struct
std::pair<int const ,class CclsHostConnection *>,struct std::map<int,class
CclsHostConnection *,struct std::less<int>,class

My map is just decalred as a int as the key and the pointer definition as
above to what is being stored.

Any help is appreciated.



Sat, 20 Sep 2003 04:45:20 GMT  
 Simple Question I am sure !!!!!
Map iterators are actually pairs; first==key,second == object;

 for (hostIterator = m_lstHostConnections.begin(); hostIterator !=
 m_lstHostConnections.end(); ++hostIterator)
  {
         hostIterator.second->UpdateStatus();
  }


Quote:
> Hi,

> I am using a STL map for the first time. I have managed to store a pointer
> to my class in the list. But I now need to get it out of the list.

> Looking at the sample on MSDN I think I am quite close however - the
> documentation is FAR from clear.

>  CclsHostConnection* pHostConnection;
>  HOSTMAP::iterator hostIterator;

>  for (hostIterator = m_lstHostConnections.begin(); hostIterator !=
> m_lstHostConnections.end(); ++hostIterator)
>  {
>       pHostConnection = (CclsHostConnection*) hostIterator;
>       pHostConnection->UpdateStatus();
>  }

> The case from the iterator back to the correct type failes with the
> following error message

> error C2440: 'type cast' : cannot convert from 'class

std::_Tree<int,struct

- Show quoted text -

Quote:
> std::pair<int const ,class CclsHostConnection *>,struct std::map<int,class
> CclsHostConnection *,struct std::less<int>,class

> My map is just decalred as a int as the key and the pointer definition as
> above to what is being stored.

> Any help is appreciated.



Sat, 20 Sep 2003 05:04:15 GMT  
 Simple Question I am sure !!!!!
Hi,

Thanks for that. You were very nearly right - but put me well enough in the
right direction I got it sorted.
You just need to dereference the pointer first - but cheers, got me what I
required.

for (hostIterator = m_lstHostConnections.begin(); hostIterator !=
 m_lstHostConnections.end(); ++hostIterator)
  {
         (*hostIterator).second->UpdateStatus();
  }


Quote:
> Map iterators are actually pairs; first==key,second == object;

>  for (hostIterator = m_lstHostConnections.begin(); hostIterator !=
>  m_lstHostConnections.end(); ++hostIterator)
>   {
>          hostIterator.second->UpdateStatus();
>   }



> > Hi,

> > I am using a STL map for the first time. I have managed to store a
pointer
> > to my class in the list. But I now need to get it out of the list.

> > Looking at the sample on MSDN I think I am quite close however - the
> > documentation is FAR from clear.

> >  CclsHostConnection* pHostConnection;
> >  HOSTMAP::iterator hostIterator;

> >  for (hostIterator = m_lstHostConnections.begin(); hostIterator !=
> > m_lstHostConnections.end(); ++hostIterator)
> >  {
> >       pHostConnection = (CclsHostConnection*) hostIterator;
> >       pHostConnection->UpdateStatus();
> >  }

> > The case from the iterator back to the correct type failes with the
> > following error message

> > error C2440: 'type cast' : cannot convert from 'class
> std::_Tree<int,struct
> > std::pair<int const ,class CclsHostConnection *>,struct
std::map<int,class
> > CclsHostConnection *,struct std::less<int>,class

> > My map is just decalred as a int as the key and the pointer definition
as
> > above to what is being stored.

> > Any help is appreciated.



Sat, 20 Sep 2003 05:40:30 GMT  
 Simple Question I am sure !!!!!

Quote:

> Thanks for that. You were very nearly right - but put me well enough in
the
> right direction I got it sorted.
> You just need to dereference the pointer first - but cheers, got me what I
> required.

Careful with your terminology - you're actually dereferencing an iterator,
not a pointer.  I've noticed that people who tend to refer to them as
pointers are more likely to accidentially use it as if it were a real
pointer.

Quote:
> for (hostIterator = m_lstHostConnections.begin(); hostIterator !=
>  m_lstHostConnections.end(); ++hostIterator)
>   {
>          (*hostIterator).second->UpdateStatus();
>   }

Note, you can replace "(*hostIterator).second" with "hostIterator->second"
if you prefer - either is appropriate, I prefer the second since it avoids
extra parenthesis (there are enough parenthesis already in C++ code...).

Ken



Sat, 20 Sep 2003 11:28:14 GMT  
 Simple Question I am sure !!!!!
I figured you didn't need your hand held <g>.


Quote:
> Hi,

> Thanks for that. You were very nearly right - but put me well enough in
the
> right direction I got it sorted.
> You just need to dereference the pointer first - but cheers, got me what I
> required.



Sun, 21 Sep 2003 03:48:15 GMT  
 Simple Question I am sure !!!!!

Quote:
> Hi,

> Thanks for that. You were very nearly right - but put me well enough in
the
> right direction I got it sorted.
> You just need to dereference the pointer first - but cheers, got me what I
> required.

> for (hostIterator = m_lstHostConnections.begin(); hostIterator !=
>  m_lstHostConnections.end(); ++hostIterator)
>   {
>          (*hostIterator).second->UpdateStatus();
>   }

I have been looking all over for an answer to this very same question
today - thanks!!


Sat, 27 Sep 2003 03:08:26 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. I still am not sure what pragma is

2. simple question, but I am stuck.

3. simple problem i'm sure

4. I'm not sure my simple logic is correct

5. I have a simple task but I am not sure which approach is correct. Please help.

6. I have a simple task but I am not sure which approach is correct. Please help.

7. I am a retard (perhaps a simple problem??)

8. a very simple problem about # include files but i am a beginer

9. Easy Question I'm sure...I need help

10. Wininet question, I am sure you have seen this?

11. Wininet question, I am sure you have seen this?

12. a simple simple question...

 

 
Powered by phpBB® Forum Software