Apr 19, 2011 at 4:48pm UTC
my code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
#include "Server.h"
//...
PLAYERSOCKET * sck2 = (PLAYERSOCKET*)(iValue + 0xC48);
std::string::size_type mapsize = Server::Players.size();
std::string keyName = ToLower( sck2->CharacterName );
Server::Players.insert( std::pair<std::string,PLAYERSOCKET*>(keyName, sck2) );
// new server connection
if (mapsize != Server::Players.size())
{
// output increases correct
OutputDebugString( "Size of our map: %d" , Server::Players.size() );
// loop through our map
for (PlayersT::iterator it = Server::Players.begin(); it != Server::Players.end(); it++)
{
// outputs "\t\t0 0 IP: 192.168.1.1"
OutputDebugString( "\t\t%d %d IP: %s" , (*it), (*it).first, (*it).second->ipaddress );
}
}
Server.h
1 2 3 4 5 6 7 8 9 10 11 12 13 14
#ifndef SERVER_H
#define SERVER_H
#include "stdafx.h"
typedef std::map<std::string,PLAYERSOCKET*> PlayersT;
class Server
{
public :
static std::map<std::string,PLAYERSOCKET*> Players;
};
#endif //SERVER_H
Server.cpp
1 2 3 4 5 6
//---------------------------------------------------------------------------
#include "Server.h"
//---------------------------------------------------------------------------
std::map<std::string,PLAYERSOCKET*> Server::Players;
the reference doc says:
1 2 3 4
map<Key,T>::iterator it;
(*it).first; // the key value (of type Key)
(*it).second; // the mapped value (of type T)
(*it); // the "element value" (of type pair<const Key,T>)
// outputs "\t\t0 0 IP: 192.168.1.1"
OutputDebugString( "\t\t%d %d IP: %s", (*it), (*it).first, (*it).second->ipaddress );
Why is the first one returning 0 ?
// This is function that is called in a gameserver and I want to be able to loop through all connected clients.
Last edited on Apr 19, 2011 at 7:00pm UTC
Apr 19, 2011 at 6:55pm UTC
Type of CharacterName? Type of ipaddress? Declaration/implementation of OutputDebugString? Can it handle std::strings?
You sure you want to use %d when you pass a pointer?
Apr 19, 2011 at 7:57pm UTC
As you can see, printf-like output is very error-prone, so you might want to consider switching your debug output to C++ stream syntax.
Last edited on Apr 19, 2011 at 7:58pm UTC