|
Hi I had a structure which looks like this:
1.
struct CMyStruct
{
5-6 CString ...;
int ...;
long ...;
1 CStringArray ...;
};
2.
My application is going to generate a lot of instances of CMyStruct; I therefore thought it to be a good idea to use the CArray MFC template class to *manage* my structs. (Adding, deleting etc.)
3.
Accordingly, I put the following definition in my dialog header file.
#include <afxtempl.h>
#include <Mystruct header file>
CArray<CMyStruct,CMyStruct&>MyStructArray;
4. After this I began using it in the following manner:
CMyStruct myStruct;
PopulateMyStruct(&myStruct);//Structure populated
MyStructArray.Add(myStruct);//ERROR PRODUCING LINE
5. On compilation i receive the following error:
\INCLUDE\afxtempl.h(443) : error C2582: 'CMyStruct' : 'operator =' function is unavailable
C:\Program Files\Microsoft Visual Studio\VC98\INCLUDE\xlocale(467) : while compiling class-template member function 'void __thiscall CArray<struct CMyStruct,struct CMyStruct &>::SetAtGrow(int,struct CMyStruct &)'
6. What could I be doing wrong??
Any help would be greatly appreciated.
|
|
|
|
|
Imlementation of SetAtGrow method uses an operator =. You need to implement one for your struct/class.
Pavel
Sonork 100.15206
|
|
|
|
|
does CMyStruct have an "operator =" member ? looks like that's what the compiler wants.
have you considered std::vector instead of CArray? overall, std::vector is a nicer way to deal with an array of things.
-c
Alcohol is the anesthesia by which we endure the operation of life.
-- George Bernard Shaw
|
|
|
|
|
hi there,
just wanted to let you'll know:
When i remove the CStringArray from the structure it works!
|
|
|
|
|
This is because of CStringArray has no "=" operator.
Best regards
Holger Persch
|
|
|
|
|
OK thats fine, but what happens when I **NEED** a CStringArray in my structure.
(Right now i have replaced the CStringArray by a char**) but thats not very elegant!)
|
|
|
|
|
then you need to write an "=" operator for your struct. in it, you'll copy all the data items from the input struct to "this".
-c
Alcohol is the anesthesia by which we endure the operation of life.
-- George Bernard Shaw
|
|
|
|
|
Do you mean implementing a = operator that copies from one CStringArray into another.
P.S:I say this, because i am now using a statically allocated char[][], thereby eliminating the need for copying!
|
|
|
|
|
Yes - you need to impliment operator = for CStringArray, as Chris has already said twice now.
As he has also said, the MFC container classes are plain ugly compared to STL, and std::vector would solve all of these problems as well as opening up a whole world of elegant container design to you.
Christian
Hey, at least Logo had, at it's inception, a mechanical turtle. VB has always lacked even that... - Shog9 04-09-2002
During last 10 years, with invention of VB and similar programming environments, every ill-educated moron became able to develop software. - Alex E. - 12-Sept-2002
|
|
|
|
|
The "=" operator can look like this:
class CMyClass
{
.
.
.
const CMyClass &operator =(const CMyClass &src)
{
m_String = src.m_String;
m_StringArray.RemoveAll();
m_StringArray.Append(src.m_StringArray);
m_StringArray.FreeExtra();
return *this;
}
public:
CString m_String;
CStringArray m_StringArray;
};
Best regards
Holger Persch
|
|
|
|
|
Thanks so much for this Holger, that was very kind of you.
|
|
|
|
|
Is there any reason for which you prefer RemoveAll/Append/FreeExtra over CStringArray::Copy?
Tomasz Sowinski -- http://www.shooltz.com
*** Si fractum non sit, noli id reficere. ***
|
|
|
|
|
No, i have just not recognized that there exists a copy method .
Best regards
Holger Persch
|
|
|
|
|
I am trying to use PreTranslateMessage to translate a key press into a button click message. This is the code but it aint working:
<br />
BOOL CMScaleDlg::PreTranslateMessage(MSG* pMsg)<br />
{<br />
if (pMsg->message == WM_KEYDOWN)<br />
{<br />
if (pMsg->wParam == VK_F2)<br />
{<br />
pMsg->message = WM_COMMAND;<br />
pMsg->wParam = BN_CLICKED;<br />
pMsg->lParam = (LPARAM) ::GetDlgItem(GetSafeHwnd(), IDC_BUTTON1);<br />
}<br />
}<br />
return CDialog::PreTranslateMessage(pMsg);<br />
}<br />
The code which modifies the pMsg is being executed but nothing happens.
|
|
|
|
|
try:
pMsg->wParam = MAKEWPARAM(IDC_BUTTON1, BN_CLICKED);
When you send a WM_COMMAND message, the wParam variable contains 2 pieces of information, the notification code (BN_CLICKED ) in the HIWORD , and the control's ID in the LOWORD .
HTH
CPUA 0x5041
Sonork 100.11743 Chicken Little
"So it can now be written in stone as a testament to humanities achievments "PJ did Pi at CP"." Colin Davies
Within you lies the power for good - Use it!
|
|
|
|
|
Still not working. I tried a different method using PostMessage and this worked:
<br />
if (pMsg->wParam == VK_F2)<br />
{<br />
PostMessage(WM_COMMAND, <br />
MAKEWPARAM(IDC_BUTTON1, BN_CLICKED), <br />
(LPARAM) ::GetDlgItem(GetSafeHwnd(), IDC_BUTTON1));<br />
return true;<br />
<br />
}<br />
I can use this method, it seems to work ok, but I am curious why modifying the pMsg parameter doesn;t seem to work.
|
|
|
|
|
MFC has this warning all around the docs:Note This member function is called by the framework to allow your application to handle a Windows message. The parameters passed to your function reflect the parameters received by the framework when the message was received. If you call the base-class implementation of this function, that implementation will use the parameters originally passed with the message and not the parameters you supply to the function. meaning it will ignore the message and parameters you give it, and instead read the original message from a cache somewhere.
--Mike--
"I'd rather you just give me a fish today, because even if you teach me how to fish, I won't do it. I'm lazy." -- Nish
Just released - 1ClickPicGrabber - Grab & organize pictures from your favorite web pages, with 1 click!
My really out-of-date homepage
Sonork-100.19012 Acid_Helm
|
|
|
|
|
hello!
my cstring looks like that:
1;2;3;.....and so on.
i want to know, how i can delete the ';' inside this string.
is there a possibility to reset the ';' into a blank (' ')?
MFC
|
|
|
|
|
Doesn't CString have a Replace function that does this ?
<Edit> I asked because I wasn't sure. Yes, it does have a Replace method, which will do what you want. </Edit>
Christian
Hey, at least Logo had, at it's inception, a mechanical turtle. VB has always lacked even that... - Shog9 04-09-2002
During last 10 years, with invention of VB and similar programming environments, every ill-educated moron became able to develop software. - Alex E. - 12-Sept-2002
|
|
|
|
|
super, thanks.
where did I have my eyes?
do u know also, how can delete the ';' without a replace?
MFC
|
|
|
|
|
Just replace them with an empty string.
Christian
Hey, at least Logo had, at it's inception, a mechanical turtle. VB has always lacked even that... - Shog9 04-09-2002
During last 10 years, with invention of VB and similar programming environments, every ill-educated moron became able to develop software. - Alex E. - 12-Sept-2002
|
|
|
|
|
i tried it, with
str.Replace(';', '');
but then i got an error message, because of the empty string.
is there another possibility?
|
|
|
|
|
str.Remove(';');
I dunno if it works for all instances, I've just been looking up the MSDN to answer you. I don't use MFC at all anymore, not for a long time.
Christian
Hey, at least Logo had, at it's inception, a mechanical turtle. VB has always lacked even that... - Shog9 04-09-2002
During last 10 years, with invention of VB and similar programming environments, every ill-educated moron became able to develop software. - Alex E. - 12-Sept-2002
|
|
|
|
|
it functions!
thank you very much!
MFC
p.s.: i never find what i´m looking for in the msdn.
|
|
|
|
|
If i draw one from the toolbox, my application won't start, it exits right after starting. And if i try to create one with CreateWindowEx and PROGRESS_CLASS as classname like msdn does, it says it's an invalid class, how do i do it?
thanks!
|
|
|
|