473,750 Members | 2,668 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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
Jul 19 '05 #1
5 29252
Christopher wrote in news:rU******** *********@twist er.austin.rr.co m:
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) );
it->second.Send( std::string(tex tbuff) );

......
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(textbuf f));

or

it->Send(std::stri ng(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

"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: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


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

....
BOOL Player::Send(st d::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_SOCKE T::iterator it;
for(it = players_by_sock et.begin(); it!= players_by_sock et.end(); ++it)
*it.Send(std::s tring(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

"Kevin Goodsell" <us************ *********@never box.com> wrote in message
news:3f4a93ba@s hknews01...
<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.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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

24
17319
by: Duane Hebert | last post by:
2 questions: Given a map defined as std::map<int,string> stringmap; //How do you access the data_type (string) via an iterator? std::string spoo("doh");
5
2758
by: Angus Leeming | last post by:
Dinkumware's online STL reference http://tinyurl.com/3es52 declares std::map's overloaded erase member functions to have the interface: map::erase iterator erase(iterator where); iterator erase(iterator first, iterator last); size_type erase(const Key& keyval); Ie, the first two functions above have the same interface as
14
4060
by: Flzw | last post by:
Well I have a map like this : std::map <string, CObject> ObjectList; I have a function like this : CObject* NewObject( char* Name, CArg* Arg) { std::string key = Name; ObjectList = CObject( Name, Arg);
17
7550
by: Gernot Frisch | last post by:
restart: for (std::map<x,y>::iterator it = m.begin(); it!=m.end(); ++it) { if( it->second.isbad() ) { std::map<x,y>::iterator next = it; ++next; m.erase(it); it=next; // goto restart;
19
6163
by: Erik Wikström | last post by:
First of all, forgive me if this is the wrong place to ask this question, if it's a stupid question (it's my second week with C++), or if this is answered some place else (I've searched but not found anything). Here's the problem, I have two sets of files, the name of a file contains a number which is unique for each set but it's possible (even probable) that two files in different sets have the same numbers. I want to store these...
1
6479
by: Avery Fong | last post by:
The following program will result in a compile error when building under Debug but will compile under Release. Why does is work under Release mode but not under Debug This program is developed under Visual Studio .NET 2003 in a Win32 Console Project // VectorInsert.cpp : Defines the entry point for the console application / #include "stdafx.h #include "VectorInsert.h #ifdef _DEBU
13
9674
by: kamaraj80 | last post by:
Hi I am using the std:: map as following. typedef struct _SeatRowCols { long nSeatRow; unsigned char ucSeatLetter; }SeatRowCols; typedef struct _NetData
20
5850
by: Dilip | last post by:
I understand the C++ standard does not talk about threading. My question here is directed more towards what happens when a STL container is used in a certain way. I'd appreciate any thoughts. I re-iterate I don't want to probe into what C++ standard says when I trample some data from multiple threads, I simply want to know if I have understood this right. I have a "std::map<somedatatype, someotherdatatype> myMap", where stuff gets...
8
4074
by: mveygman | last post by:
Hi, I am writing code that is using std::map and having a bit of an issue with its performance. It appears that the std::map is significantly slower searching for an element then a sequential search in a vector. Has anyone run into this before?
0
9000
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9396
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9339
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9256
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8260
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6804
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6081
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4713
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4887
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.