PocketMatrix forums

A read-only archive of the PocketMatrix forums (2001–2014). Posting and accounts are closed.

CString : I'm ashamed

Hi, I'm so sorry to ask you an obvious question.

I want to use CStrings in my eVC++ project.
I've put the following includes :
#include <afx.h>
#include <afxwin.h>
but when I put somewhere the declaration "CString c;"
I get the following compilation error:
"uafxwce.lib(afxmem.obj) : error LNK2005: "void __cdecl operator delete(void *)" (??3@YAXPAX@Z) already defined in coredll.lib(COREDLL.dll)
uafxwce.lib(afxmem.obj) : warning LNK4006: "void __cdecl operator delete(void *)" (??3@YAXPAX@Z) already defined in coredll.lib(COREDLL.dll); second definition ignored
ARMRel/MyARM.exe : fatal error LNK1169: one or more multiply defined symbols found
Error executing link.exe."

Please help cause I'm becoming may about this.
Of course, if I remove the line "CString c;", the program compiles perfectly...
grrrr
CString is part of MFC. You have to create an MFC app to use it.

Dan East
Alfredo wrote:
Of course, if I remove the line "CString c;", the program compiles perfectly...
grrrr


Does the include line compile with the unwanted semicolon? Anyway best leave CString well alone as it is pants and for people who for some reason think it and MFC makes life easier for them.... you can do the same with the functions in string.h. I never understand why people bother with MFC, isn't C++ and all the library functions hard enough to master without having to learn MFC on top. You only spend hours trying to overide MFC functionality in the end to do something that should only take a couple of minutes without. My advice stay well away...
Which is simpler and more intuitive?

Code:

//Using CString class:

CString s="is a";
s+=" test.";
AfxMessageBox("This "+s); //Display "This is a test."

//Using string functions

char buf[256];
strcpy(buf, "is a");
strcat(buf, " test.");
//Have to use another buffer to insert text
char buf2[256];
strcpy(buf2, "This ");
strcat(buf2, buf);
//Now we have to manually convert to unicode
TCHAR t_buf[256];
MultiByteToWideChar(CP_ACP, NULL, buf2, -1, t_buf, sizeof(t_buf)/sizeof(TCHAR));
//Now we can finally display our string
AfxMessageBox(t_buf); //Display "This is a test."


That example is a bit contrived, but you can certainly see the usefulness of a string class. Technically speaking, if you use standard Windows API and string routines as you indicated, you are programming in C, not C++ (unless you are creating your own wrapper classes).

Dan East
Aren't there any non-MFC string classes?
here is a page with a link to the AP Computer Science string class http://www.amberman.org/evolve/Software/strings/

I don't really know if it could replace CString without much work, but it looks like a good start.
Hmm, I thought that comparison was a bit unfair...

Code:

CString s="is a";
s+=" test.";
AfxMessageBox("This "+s); //Display "This is a test."


could be written as

Code:

TCHAR string[64];
wsprintf(string,L"This %.40S test.","is a");
MessageBox(hWnd,string,L"",MB_OK); //Display "This is a test."


without ATL/MFC or any of the associated size of the libraries. No object creation and destruction churn either, and all within COREDLL.
Um, I was showing the ease in which string copies, concatenations and UNICODE conversions can be done with CString. If your purpose is to optimize a contrived example then the correct response is:

AfxMessageBox(_T("This is a test"));

CString::Format performs the same functionality of sprintf, again with automatic UNICODE conversion.

Dan East
Using coredll only, string copies can be done with strcpy, concatenations with strcat and UNICODE conversions with mcstowcs.

Dan East wrote:
Um, I was showing the ease in which string copies, concatenations and UNICODE conversions can be done with CString. If your purpose is to optimize a contrived example...


No not at all, I was just attempting to copy your example using standard coredll functions only. It wasn't contrived and will work in a similar way only much faster (no object churn) with a smaller program size and less DLL dependancy.

However what you seem to be doing is advocating a design strategy (CString-based) by only allowing THAT design strategy in your examples! This I think, is an unfair comparison.
My example was in response to pacpmoc:

pacomac wrote:
Anyway best leave CString well alone as it is pants and for people who for some reason think it and MFC makes life easier for them.... you can do the same with the functions in string.h. I never understand why people bother with MFC...


I'm not advocating a design strategy or anything else. I was just showing the usefulness of CString and how it can "make life easier" using a contrived example. It should be obvious in such an example that the various string operations would take place at differing times and in other routines - not all serially. The use of printf in all those cases would be as bad as or worse than instantiating C++ classes. There is certainly a purpose in using CString, just as there is a purpose in using C instead of assembly. According to your argument C is unnecessary, because we can accomplish the same thing in assembly.

That said, I do not use CString. There is a fine balance between higher level languages (OOP) and performance. I always choose performance, which is the reason I don't program in Java or VB .

Dan East
Dan East wrote:
There is certainly a purpose in using CString, just as there is a purpose in using C instead of assembly. According to your argument C is unnecessary, because we can accomplish the same thing in assembly.


Now hang on...! I'm not disputing that *should* you want BASIC/Java-like manipulation of string objects CString is the way to go.

But I think pacomac's point was that 99% of CString's functionality is in the standard ANSI functions.

And at what point did I argue C or C++ is unnecessary??!
I wrote a simple string class ( with help from the link above, as well as a few peeks at the MFC CString source ) and, while it is by no means optimized, it is definitely faster and more convenient to code with. It is worlds easier to be able to

Code:
GString str, str2; //all my classes start with G

str = "This "; //ascii to unicode
strcpy(str2, str); //...and back

str += L"is a test.";
str2 = 100; //easy number/string conversion
str2 += 'a'; //complete with single char concatenation

if (str2 == "100a")
MessageBox(NULL, str, str2, MB_OK); //"100a" "This is a test."


Memory is dynamically allocated with each change so no memory sits empty. On destruction all memory is freed. Very convenient. The only catch I've come across so far is you sometimes have to cast the number/string conversion to (int) when assigning a value from a function that returns a size_t, etc.
If you want a string class that is thourougly tested, optimized and works pretty much like Javas String (or CString although I have never used MFC because I think it really sucks), then the STL string container is the answer.

STLport (www.stlport.org) is a free available STL source library that works on practically any platform. Just make shure you disable the iostream support first. Info on how to do this can be found on www.pocketfrog.com and on the PocketFrog forum here on PocketMatrix.