std::map iterator

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Christopher

    #1

    std::map iterator

    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_SOCKE T;

    .....
    BOOL Player::Send(st d::string data)
    ......
    // Send the text to all clients
    PLAYERMAP_SOCKE T::iterator it;
    for(it = players_by_sock et.begin(); it!= players_by_sock et.end(); ++it)
    *it.Send(std::s tring(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


  • Rob Williscroft

    #2
    Re: std::map iterator

    Christopher wrote in news:rUo2b.799$ f76.26049@twist er.austin.rr.co m:
    [color=blue]
    > 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_SOCKE T;
    >
    > ....
    > BOOL Player::Send(st d::string data)
    > .....
    > // Send the text to all clients
    > PLAYERMAP_SOCKE T::iterator it;
    > for(it = players_by_sock et.begin(); it!= players_by_sock et.end();
    > ++it)
    > *it.Send(std::s tring(textbuff) );[/color]

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

    [color=blue]
    > ......
    > error C2039: 'Send' : is not a member of 'pair<unsigned int const
    > ,class
    >Player>'
    >[/color]

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

    (*it).Send(std: :string(textbuf f));

    or

    it->Send(std::stri ng(textbuff));

    should produce that error.
    [color=blue]
    > how do I call the function from the "pair"?
    >[/color]

    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.
    --

    Comment

    • Christopher

      #3
      Re: std::map iterator


      "Agent Mulder" <mbmulder_remov e_this_@home.nl > wrote in message
      news:bid8ep$dfr $1@news2.tilbu1 .nb.home.nl...[color=blue]
      > 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:S end(std::string ){}};//which one is sending?
      > class Player{public:S end(std::string ){}};//which one is sending?
      > int main(int,char** )
      > {
      > std::map<SOCKET ,Player>players _by_socket;
      > for
      > (
      > std::map<SOCKET ,Player>::itera tor it=players_by_s ocket.begin();
      > it!=players_by_ socket.end();
      > ++it
      > )
      > {
      > it->first.Send(std ::string());
      > it->second.Send(st d::string());
      > }
      > return 0;
      > }
      >
      > -X[/color]

      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


      Comment

      • Kevin Goodsell

        #4
        Re: std::map iterator

        Christopher wrote:
        [color=blue]
        > 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_SOCKE T;
        >
        > ....
        > BOOL Player::Send(st d::string data)[/color]

        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?
        [color=blue]
        > .....
        > // Send the text to all clients
        > PLAYERMAP_SOCKE T::iterator it;
        > for(it = players_by_sock et.begin(); it!= players_by_sock et.end(); ++it)
        > *it.Send(std::s tring(textbuff) );[/color]

        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.

        Comment

        • Christopher

          #5
          Re: std::map iterator


          "Kevin Goodsell" <usenet1.spamfr ee.fusion@never box.com> wrote in message
          news:3f4a93ba@s hknews01...
          <snip>[color=blue]
          > 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?[/color]

          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


          Comment

          • Ron Natalie

            #6
            Re: std::map iterator


            "P.J. Plauger" <pjp@dinkumware .com> wrote in message news:3f4b6a51$0 $19407$afc38c87 @...
            [color=blue]
            > 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.[/color]

            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.


            Comment

            Working...