I''m trying to do something that I think ought be very simple... but I just don''t now enough about adaptors to understand what exactly I need to do. How do I use the STL adaptors to call a method on a range of iterators _and pass that method a parameter?
This is my conventional loop...
vector<udpPacket> m_RecvPacketPool(32);
m_RecvPacketPool.resize(32);
for(int i=0; i<100; ++i)
m_RecvPacketPool[i].Alloc(2048);
...that I''d like to replace with an STL for_each function
These are my feable attemps:
for_each(m_RecvPacketPool.begin(), m_RecvPacketPool.end(), mem_fun1_ref(udpPacket::Alloc, 2048));
for_each(vRecvPacketPool.begin(), vRecvPacketPool.end(), bind1st(mem_fun1_ref(udpPacket::Alloc), 2048));
for_each(vRecvPacketPool.begin(), vRecvPacketPool.end(), bind2nd(mem_fun1_ref(udpPacket::Alloc), 2048));
neither of which work
(MSVC6 with the built-in STL)
Here''s the diagnostic mess:
quote:
c:\program files\microsoft\visual studio 6\vc98\include\algorithm(37) : error C2664: ''()'' : cannot convert parameter 1 from ''struct I3D::Socket::udpPacket'' to ''const unsigned int &''
Reason: cannot convert from ''struct I3D::Socket::udpPacket'' to ''const unsigned int''
No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
E:\Projects\MSVC\SocketTest\SocketTestDlg.cpp(77) : see reference to function template instantiation ''class std::binder1st > __cdecl std::for_each(struct I3D::Socket::
udpPacket *,struct I3D::Socket::udpPacket *,class std::binder1st >)'' being compiled
c:\program files\microsoft\visual studio 6\vc98\include\algorithm(37) : error C2064: term does not evaluate to a function
E:\Projects\MSVC\SocketTest\SocketTestDlg.cpp(77) : see reference to function template instantiation ''class std::binder1st > __cdecl std::for_each(struct I3D::Socket::
udpPacket *,struct I3D::Socket::udpPacket *,class std::binder1st >)'' being compiled
Here''s the signiture of the Alloc & Test methods:
int Alloc(u32 cBytes);
int Test();
This does work...
for_each(vRecvPacketPool.begin(), vRecvPacketPool.end(), mem_fun_ref(udpPacket::Test));
...it doesn''t do anything useful, but show that I figured out how to call a method with no parameters
(And before some smart-ass drops the "search google" thing on me, there''s only
two hits - one''s a post-script file and the other is in Japanese!)
Magmai Kai Holmlor
"Oh, like
you''ve never written buggy code" - Lee
"What
I see is a system that _could do anything - but currently does
nothing !" - Anonymous CEO
- The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara