By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,305 Members | 1,588 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,305 IT Pros & Developers. It's quick & easy.

std::map iterator

P: n/a
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
Jul 19 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Christopher wrote in news:rU*****************@twister.austin.rr.com:
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::pair< 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/
Jul 19 '05 #2

P: n/a

"Agent Mulder" <mb*******************@home.nl> wrote in message
news:bi**********@news2.tilbu1.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
Jul 19 '05 #3

P: n/a
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.

Jul 19 '05 #4

P: n/a

"Kevin Goodsell" <us*********************@neverbox.com> 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
Jul 19 '05 #5

P: n/a

"P.J. Plauger" <pj*@dinkumware.com> 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.
Jul 19 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.