Strange behavior of compiler [modified]
-
Hi experts, I faced following situation sometimes... I am using VS 2005. Sometimes Compiler forgets to warn or raise an error about inappropriate string initialization or functions or other string manipulation. For example:
CString str = ”Hello”;
Gives error… and I correct this by prefixing “L”CString str = L”Hello”;
But sometimes somewhere in my code inside any function or loopCString otherStr = ”Hello world”;
Gives neither warning nor error and runs correctly. And when i change/modify that stringCString otherStr = ”Hello world”;
it gives error. Finally i correct like thisCString otherStr = L”Hello world”;
I am unable to understand this.please help me to understand this. Thanksmodified on Friday, May 16, 2008 5:59 AM
-
Hi experts, I faced following situation sometimes... I am using VS 2005. Sometimes Compiler forgets to warn or raise an error about inappropriate string initialization or functions or other string manipulation. For example:
CString str = ”Hello”;
Gives error… and I correct this by prefixing “L”CString str = L”Hello”;
But sometimes somewhere in my code inside any function or loopCString otherStr = ”Hello world”;
Gives neither warning nor error and runs correctly. And when i change/modify that stringCString otherStr = ”Hello world”;
it gives error. Finally i correct like thisCString otherStr = L”Hello world”;
I am unable to understand this.please help me to understand this. Thanksmodified on Friday, May 16, 2008 5:59 AM
all depends if you have unicode enabled in you project settings or not. when you assign litterals to CStrings, you have to use
_T()
orTEXT()
macros.CString str = _T("Hello"); //OK
CStringA strAnsi = "Hello"; //OK
CStringW strUni = L"Hello" //OKthe
_T()
macros will expand the literal text to either ansi or unicode regarding your project settings... for your knowledge, here is how it's defined (kinda) :#ifdef _UNICODE
#define _T(x) L##x
#else
#define _T(x) x
#endif[VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]
-
all depends if you have unicode enabled in you project settings or not. when you assign litterals to CStrings, you have to use
_T()
orTEXT()
macros.CString str = _T("Hello"); //OK
CStringA strAnsi = "Hello"; //OK
CStringW strUni = L"Hello" //OKthe
_T()
macros will expand the literal text to either ansi or unicode regarding your project settings... for your knowledge, here is how it's defined (kinda) :#ifdef _UNICODE
#define _T(x) L##x
#else
#define _T(x) x
#endif[VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]
Sorry i forgot to say that when i change/modify that string
CString otherStr = ”Hello world”;
it gives error.But sometimes somewhere in my code inside any function or loop CString otherStr = ”Hello world”; Gives neither warning nor error and runs correctly.
I mean if i touch that string, it starts to give error. And finally i correct like this
CString otherStr = L”Hello world”;
-
Sorry i forgot to say that when i change/modify that string
CString otherStr = ”Hello world”;
it gives error.But sometimes somewhere in my code inside any function or loop CString otherStr = ”Hello world”; Gives neither warning nor error and runs correctly.
I mean if i touch that string, it starts to give error. And finally i correct like this
CString otherStr = L”Hello world”;
Best way is to use _T macro. It will take care of everything. Cstring str= _T("test it");
-
Best way is to use _T macro. It will take care of everything. Cstring str= _T("test it");
you are right.:) but i was talking not only string intialization also about functions like:
strcpy()
also shows the same behavior. it accepts sometimes CString parameters while i am working with unicode settings. -
you are right.:) but i was talking not only string intialization also about functions like:
strcpy()
also shows the same behavior. it accepts sometimes CString parameters while i am working with unicode settings.it's because strcpy() is accepting ansi strings (char*) only, and if you have unicode defined, your CStrings convert implicitely to unicode string, not ansi (wchar_t*)
[VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]
-
all depends if you have unicode enabled in you project settings or not. when you assign litterals to CStrings, you have to use
_T()
orTEXT()
macros.CString str = _T("Hello"); //OK
CStringA strAnsi = "Hello"; //OK
CStringW strUni = L"Hello" //OKthe
_T()
macros will expand the literal text to either ansi or unicode regarding your project settings... for your knowledge, here is how it's defined (kinda) :#ifdef _UNICODE
#define _T(x) L##x
#else
#define _T(x) x
#endif[VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]
But why would the compiler complain about one and not the other? The OP stated that it only starts to complain about that other (second) one if it is modified?
"Love people and use things, not love things and use people." - Unknown
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
-
But why would the compiler complain about one and not the other? The OP stated that it only starts to complain about that other (second) one if it is modified?
"Love people and use things, not love things and use people." - Unknown
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
DavidCrow wrote:
But why would the compiler complain about one and not the other? The OP stated that it only starts to complain about that other (second) one if it is modified?
Right. Now only you understand my question. I also want to know. Please sir, explain me about this strange behavior of complier. BTW what is OP?
DavidCrow wrote:
The OP stated that
:)