std::map iterator

Discussion in 'C++' started by Christopher, Aug 25, 2003.

  1. Christopher

    Christopher Guest

    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
    Christopher, Aug 25, 2003
    #1
    1. Advertising

  2. Christopher wrote in news:rUo2b.799$:

    > 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/
    Rob Williscroft, Aug 25, 2003
    #2
    1. Advertising

  3. Christopher

    Christopher Guest

    "Agent Mulder" <> wrote in message
    news:bid8ep$dfr$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
    Christopher, Aug 25, 2003
    #3
  4. 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.
    Kevin Goodsell, Aug 25, 2003
    #4
  5. Christopher

    Christopher Guest

    "Kevin Goodsell" <> 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
    Christopher, Aug 26, 2003
    #5
  6. Christopher

    Ron Natalie Guest

    "P.J. Plauger" <> 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.
    Ron Natalie, Aug 26, 2003
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Peter Jansson
    Replies:
    5
    Views:
    6,272
    Ivan Vecerina
    Mar 17, 2005
  2. Replies:
    6
    Views:
    634
    Jim Langston
    Oct 30, 2005
  3. Replies:
    1
    Views:
    408
    red floyd
    Dec 21, 2008
  4. Thomas J. Gritzan
    Replies:
    6
    Views:
    999
    James Kanze
    Dec 22, 2008
  5. James Kanze
    Replies:
    0
    Views:
    1,983
    James Kanze
    Dec 21, 2008
Loading...

Share This Page