Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > std::map iterator

Reply
Thread Tools

std::map iterator

 
 
Christopher
Guest
Posts: n/a
 
      08-25-2003
I am having problems iterating through the sequence of objects stored in my
std::map I want to call a function from the "value" object in the map. Here
is my snippet:

typedef std::map<SOCKET, Player> PLAYERMAP_SOCKET;

.....
BOOL Player::Send(std::string data)
......
// Send the text to all clients
PLAYERMAP_SOCKET::iterator it;
for(it = players_by_socket.begin(); it!= players_by_socket.end(); ++it)
*it.Send(std::string(textbuff));
.......
error C2039: 'Send' : is not a member of 'pair<unsigned int const ,class
Player>'

how do I call the function from the "pair"?

Thanx,
Christopher


 
Reply With Quote
 
 
 
 
Rob Williscroft
Guest
Posts: n/a
 
      08-25-2003
Christopher wrote in news:rUo2b.799$(E-Mail Removed):

> I am having problems iterating through the sequence of objects stored
> in my std::map I want to call a function from the "value" object in
> the map. Here is my snippet:
>
> typedef std::map<SOCKET, Player> PLAYERMAP_SOCKET;
>
> ....
> BOOL Player::Send(std::string data)
> .....
> // Send the text to all clients
> PLAYERMAP_SOCKET::iterator it;
> for(it = players_by_socket.begin(); it!= players_by_socket.end();
> ++it)
> *it.Send(std::string(textbuff));


it->second.Send( std::string(textbuff) );


> ......
> error C2039: 'Send' : is not a member of 'pair<unsigned int const
> ,class
>Player>'
>


Are you sure. In the above you're calling Send( ... ) on a
std::map< SOCKET, Player >::iterator:

(*it).Send(std::string(textbuff));

or

it->Send(std::string(textbuff));

should produce that error.

> how do I call the function from the "pair"?
>


The iterator for your map (conceptualy) points to a
std:air< SOCKET const, Player > a pair has two elements 'first'
and 'second'. In this case 'first' is your map's key (constant)
and 'second' is your map's value (non-constant).


HTH

Rob.
--
http://www.victim-prime.dsl.pipex.com/
 
Reply With Quote
 
 
 
 
Christopher
Guest
Posts: n/a
 
      08-25-2003

"Agent Mulder" <(E-Mail Removed)> wrote in message
news:bid8ep$dfr$(E-Mail Removed)1.nb.home.nl...
> Ch> I am having problems iterating through the
> Ch> sequence of objects stored in my std::map
> Ch> I want to call a function from the "value"
> Ch> object in the map. Here is my snippet:
> [SNIP]...
>
> #include<map>
> class SOCKET{public:Send(std::string){}};//which one is sending?
> class Player{public:Send(std::string){}};//which one is sending?
> int main(int,char**)
> {
> std::map<SOCKET,Player>players_by_socket;
> for
> (
> std::map<SOCKET,Player>::iterator it=players_by_socket.begin();
> it!=players_by_socket.end();
> ++it
> )
> {
> it->first.Send(std::string());
> it->second.Send(std::string());
> }
> return 0;
> }
>
> -X


I think I might have confused some people with my code. SOCKET is not a
class, it's just an integer defined in a 3rd party lib. The Send() function
is a member of the Player class. The intention of making the map is that
when data comes in the program is only provided with the SOCKET, so I use
the map to look up the associated player connected to that socket. So I used
SOCKET as the key and Player as the value. Of course in this case I want to
send to all players so I am just iterating through the enitire list. At any
rate the replies I got provided the needed information: it->second.Send()
and everything is working with the desired effect now.
Thanx for the helps guys,
Christopher


 
Reply With Quote
 
Kevin Goodsell
Guest
Posts: n/a
 
      08-25-2003
Christopher wrote:

> I am having problems iterating through the sequence of objects stored in my
> std::map I want to call a function from the "value" object in the map. Here
> is my snippet:
>
> typedef std::map<SOCKET, Player> PLAYERMAP_SOCKET;
>
> ....
> BOOL Player::Send(std::string data)


Why are you using BOOL when the language provides the bool type? Is it
an attempt to obfuscate the code or increase the probability of bugs?

> .....
> // Send the text to all clients
> PLAYERMAP_SOCKET::iterator it;
> for(it = players_by_socket.begin(); it!= players_by_socket.end(); ++it)
> *it.Send(std::string(textbuff));


This would be wrong even if 'it' pointed to an object that had a member
called 'Send' (which it obviously doesn't). What you have is equivalent to:

*(it.Send(std::string(textbuff)));

See the problem?

-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.

 
Reply With Quote
 
Christopher
Guest
Posts: n/a
 
      08-26-2003

"Kevin Goodsell" <(E-Mail Removed)> wrote in message
news:3f4a93ba@shknews01...
<snip>
> Why are you using BOOL when the language provides the bool type? Is it
> an attempt to obfuscate the code or increase the probability of bugs?


You'll have to ask Microsoft that one. If it was up to me I'd be happy with
the types already provided to me.
,
Christopher


 
Reply With Quote
 
Ron Natalie
Guest
Posts: n/a
 
      08-26-2003

"P.J. Plauger" <(E-Mail Removed)> wrote in message news:3f4b6a51$0$19407$afc38c87@...

> Some of us had to write C++ code that used Boolean variables *before*
> type bool was added to the draft C++ Standard. So the answer is doubtless
> `none of the above'. Unless you choose to be aggressively uncharitable.


Some of us had to program C before structs could be assigned or even before
the member names were scoped to struct. Old habits die hard.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
List iterator assignment fails, assert iterator not dereferencable David Bilsby C++ 5 10-09-2007 02:05 PM
What makes an iterator an iterator? Steven D'Aprano Python 28 04-20-2007 03:34 AM
Difference between Java iterator and iterator in Gang of Four Hendrik Maryns Java 18 12-22-2005 05:14 AM
How to convert from std::list<T*>::iterator to std::list<const T*>::iterator? PengYu.UT@gmail.com C++ 6 10-30-2005 03:31 AM
Iterator doubts, Decision on Iterator usage greg C++ 6 07-17-2003 01:26 PM



Advertisments