|
I am trying to use this:
for_each(stringname.begin(),stringname.end(),bind1st(MyFunctionName, Arg2);
this is not passing Arg2 to MyFunctionName(Arg1, Arg2)..
I am getting the following errors:
error C2825: '_Fn2::second_argument_type': cannot form a qualified name
error C2039: 'second_argument_type' : is not a member of 'operator``global namespace'''
I can implement this easily as a functor if I make a class for it, but I thought you could use a function and just use it's name without any arguments using bind1st... am I wrong?
Thx.
|
|
|
|
|
I think you need to use ptr_fun() to get a functor.
Stability. What an interesting concept. -- Chris Maunder
|
|
|
|
|
suppose i have a map<string,int> m;
if i need to search for the location using the int data im currently doing
map<string,int>::iterator iter;
for(iter=m.begin(); iter !=m.end(); iter++)
{
if(iter->second == 10)
break;
}
can i replace this with something like this
iter = find_if(m.begin(),m.end(),bind2nd(KeyEquals(),10));
in which case what would the KeyEquals function object be like???
|
|
|
|
|
Something like this should do the trick
<code>
typedef std::map<int, std::string> MyMap;
bool KeyEquals(MyMap::value_type value, int keyValue)
{
return value.first == keyValue;
}
std::string blah(MyMap const& theMap, int thing)
{
MyMap::const_iterator it =
std::find_if(theMap.begin(), theMap.end(),
std::bind2nd(std::ptr_fun(KeyEquals), thing));
return (it==theMap.end())?std::string():it->second;
}
</code>
PS - try out www.boost.org.Bind. This makes function binding a lot easier. You'd have the code below instead...
std::string blah(MyMap const& theMap, int thing)
{
MyMap::const_iterator it =
std::find_if(theMap.begin(), theMap.end(),
boost::bind(&KeyEquals, _1, thing));
return (it==theMap.end())?std::string():it->second;
}
i.e. no std::ptr_fun, no having to say that you're binding the second parameter. You can see that the order of the parameters passed to bind matches the function signature order of KeyEquals, with '_1' (a predefined placeholder) denoting that the first (and only, in this case) parameter of the bound function object is the value passed by find_if.
HTH!
Stuart Dootson
'Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p'
|
|
|
|
|
thankx for that one
it worked swell
im just into STLs now and most of the adapter tutorials seem to have bind1st operating only on vectors
so was just tweaking around with maps n function adaptors
i ran into another problem....trying to use the solution above in "transform"
suppose i get my data as a space seperated string list
say a vector of
"one 1"<br />
"two 2"<br />
"tri 3"<br />
"for 4"<br />
"fiv 5"<br />
and have to parse it into a map<string,string>
i tried using the transform function like below
typedef std::map MyMap;
typedef std::vector MyVector;
MyMap::value_type ParsetoMap(string str)
{
string::size_type index = str.find(' ');
MyMap::value_type MyMapPair;
MyMapPair.first = str.substr(0, index);
MyMapPair.second = str.substr(index+1, (int)str.length());
return MyMapPair;
}
void blah2(MyVector & theVec, MyMap const& theMap)
{
transform(theVec.begin(), theVec.end(), theMap.begin(), ptr_fun(ParsetoMap));
}
but it doesnt work
???
|
|
|
|
|
transform(theVec.begin(), theVec.end(), std::inserter(theMap, theMap.begin()), &ParsetoMap);
Stuart Dootson
'Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p'
|
|
|
|
|
say i have a data that i should be able to lookup using EITHER a int key or a string key..i repeat EITHER..
what kind of container should i be using
|
|
|
|
|
Boost.multi_index[^] should do the job.
Otherwise, you'll need to have a container for the data and separate containers for the mappings (that's what multi_index does internally, I believe).
Stuart Dootson
'Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p'
|
|
|
|
|
Hi,
I'm using CRebarCtrl to hold my commandbar, the menu's though appear as toolbar button but with the menu text, when you click nothing happens!
I used CComand bar class from the the OfficeXP Menu article http://www.codeproject.com/wtl/menuxp_wtl.asp#xx135189xx[^]
Since my app isn't based on CMainFrame, i didn't have a a standard menu, so i created CMenu on the fly and attached it to CCommandBarCtrl does this have anything to do with it.
|
|
|
|
|
hello everybody!
i habe an urgent problem!
there are 2 components i developed in Visual Studio 6 with ATL.
i used the standard wizard to create the DLL´s.
Short explaination of the 2 components:
1. Modul: Search: this dll makes a search against a data-file. it has got interface can used by the most common program languages.
2. Modul: SearchOverXML(using MSXML 4 SP2): this dll receive the parameters used for Modul 1 from a Client, receive a Object, which contains the Result from Modul 1 and the Modul 2 will write the Result-Data in a specific XML-Structur to the Client.
and now my problem. if i call Modul 2 10000 times from a client, the memory is rising and rising and if memory is out the application crashes.
i cleanup all references to MSXML and if i want to call the Release-Method on the Modul 1 Object (i forgot it at first) the Client crashes and the Memory-Leak is still there.
if i call modul 1 only, i have got no problem!
so, i dont know what to do know. maybe the implementation of Modul 2 is wrong. Has somebody a Code Example how to call a ATL Server from a other ATL Server?
thanks for help
best regards
andreas
|
|
|
|
|
I am using a SpinButton control to let the user select a value. If the user selects a new value then I want a listctrl to immediately repaint itself to show a response to this value change. The repainting only occurs when I scroll down the list control. I am using C++ on Visual Studio 2003 .NET.
My question is how do I force the list control to repaint itself, so that I don't have to scroll up or down to get a repaint? I have tried sending messages to the list ctrl and even invalidating the client area of the list ctrl.
I am the handsome one in the crowd.
|
|
|
|
|
|
I want to transfer a large data buffer, maybe 10k. Who can tell me how to realize my idea?
|
|
|
|
|
|
thanks a lot, please tell me , if I have a data buffer like "BYTE* p = myData;// there are many 0 values in buffer", I know buffer 's length, how to transfer the data use BSTR?
|
|
|
|
|
Don't treat the BSTR as a null-terminated string!
Use SysAllocStringByteLen to allocate the space.
Cast the BSTR to BYTE* and use SysStringByteLen to get the byte count.
Free the string as normal with SysFreeString .
MSDN does say that a SAFEARRAY with element type VT_UI1 is preferred, in order that you don't get translations on 16/32-bit borders...but a) who uses 16-bit OLE now?, and b) BSTR is a lot easier in C++
Stuart Dootson
'Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p'
|
|
|
|
|
It seems as if the method does not work. Following my code :
1:declare interface------
dispinterface _IMyEvents
{
properties:
methods:
[id(1), helpstring("method Test")] HRESULT Test([in]DWORD dwLen, [in] BSTR pData);
};
2:method implement-------
HRESULT Fire_Test(ULONG dwLen, BSTR pData)
{
CComVariant varResult;
T* pT = static_cast<t*>(this);
int nConnectionIndex;
CComVariant* pvars = new CComVariant[2];
int nConnections = m_vec.GetSize();
for (nConnectionIndex = 0; nConnectionIndex < nConnections; nConnectionIndex++)
{
pT->Lock();
CComPtr<iunknown> sp = m_vec.GetAt(nConnectionIndex);
pT->Unlock();
IDispatch* pDispatch = reinterpret_cast<idispatch*>(sp.p);
if (pDispatch != NULL)
{
VariantClear(&varResult);
pvars[1] = (int)dwLen;
pvars[0] = pData;
DISPPARAMS disp = { pvars, NULL, 2, 0 };
pDispatch->Invoke(0x2, IID_NULL, LOCALE_USER_DEFAULT, DISPATCH_METHOD, &disp, &varResult, NULL, NULL);
}
}
delete[] pvars;
return varResult.scode;
}
3:call method in Server Object------
{
char a[10] = _T("abcdef");
a[8] = 'x';
BSTR bstr = SysAllocStringByteLen(a, 10);
Fire_Test(10, bstr);
}
4:called in Client -------
long CEventNotifySink::OnTest(DWORD dwLen, BSTR pData)
{
UINT len = SysStringByteLen (pData); // result: len!=10
BYTE * p = (BYTE*)pData; // pData refs wrong address
SysFreeString(pData);
return 0;
}
where is wrong?
|
|
|
|
|
The problem is CComVariant - that uses SysAllocString to copy the BSTR. The signature of SysAllocString is BSTR SysAllocString(const OLECHAR * sz); - it assumes that the input BSTR is null-terminated.
If you create your VARIANT manually, you shouldn't have a problem:
VARIANT v;
V_VT(v) = VT_BSTR;
V_BSTR(v) = pData;
Stuart Dootson
'Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p'
|
|
|
|
|
not only length, the "pData" refer wrong address, data is wrong.
|
|
|
|
|
Creating the CComVariant *copied* the BSTR - i.e. created a new one. It will therefore have a different address from the original.
Stuart Dootson
'Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p'
|
|
|
|
|
I trace my my code and find that, in ATL,
COleDispatchImpl::Invoke(...) method calls CallMemberFunc(...),
and CallMemberFunc() method calls PushStackArgs(...),
there is such a sentence----"pArgTemp->bstrVal = AfxBSTR2ABSTR(pArg->bstrVal);" in PushStackArgs(...) method, After this call happened, my buffer data lost .
how to resolve the problem?
|
|
|
|
|
Hmmmm - COleDispatchImpl is part of MFC, not ATL...
Anyway - I've built a sample COM object (with connection point) using ATL and a harness executable (built using ATL to implement the connection point interface). This passes a BSTR as the event parameter and it seems to work fine...
I've got this code to fire the event:
STDMETHODIMP CPooh::Trigger()
{
BSTR bs;
BYTE a[10] = { 0x0, 0x1, 0x2, 0x3, 0x4, 0x0, 0x1, 0x2, 0x3, 0x4 };
bs = ::SysAllocStringByteLen((const char*)a, 10);
Fire_Pooh(bs);
return S_OK;
}
and this code catching the event:
void __stdcall OnPooh(BSTR bs)
{
size_t s = ::SysStringByteLen(bs);
BYTE* data = (BYTE*)bs;
}
I'm getting s set to 10, and data containing the 10 bytes set in the Trigger method.
As I said - I'm using ATL code all the way and it works fine...
Stuart Dootson
'Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p'
|
|
|
|
|
thank you, Stuart Dootson. I wrote client with MFC, there must be something wrong in my code. Later I wrote a new client with ATL, everything works fine.
|
|
|
|
|
samfromcn wrote:
something wrong in my code
I'd bet that it's in the MFC code - that's why I use ATL for COM code
Stuart Dootson
'Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p'
|
|
|
|
|
Hi, is there an "easy" way to save the state of an object to a file without using MFC or COM objects?
im working on a WTL project that needs to save and read a special form of "document" to file, (kind of like MFC´s Serialize()), but even tho i have the code to do it in binary mode, i would prefer to do it on XML.
The documents are quite large, and consists of other objects containing several arrays of CStrings (WTL:CString that is), and primitives like ints, floats, etc.
Is there a Non-MFC, Non-COM related XML parser i could use?
thanks!
The only stupid question is that one wich is never asked
|
|
|
|
|