[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Convert  "CString" to "String"

Posted on 2008-11-19
9
Medium Priority
?
2,746 Views
Last Modified: 2013-12-14
I'm use Visual Studio 2oo5.  I'm use "mfc  Visual C++" . How can I convert  data type "CString" to datatype "string" ?
0
Comment
Question by:shaf81
  • 5
  • 4
9 Comments
 
LVL 31

Expert Comment

by:Zoppo
ID: 22993888
Hi shaf81;

if you mean STL's string class with 'string' you can do simply like this:

CString cStr = "Hello, world!";
std::string str = (LPCTSTR)cStr;

Hope that helps,

ZOPPO
0
 
LVL 3

Author Comment

by:shaf81
ID: 22994411
output:-  

>------ Build started: Project: dialler1, Configuration: Debug Win32 ------
1>Compiling...
1>dialler1Dlg.cpp
1>e:\sip\sip_2\dialler1\dialler1\dialler1dlg.cpp(619) : error C2039: 'CString' : is not a member of 'std'
1>e:\sip\sip_2\dialler1\dialler1\dialler1dlg.cpp(619) : error C2440: 'initializing' : cannot convert from 'const char [14]' to 'ATL::CStringT<BaseType,StringTraits>'
1>        with
1>        [
1>            BaseType=wchar_t,
1>            StringTraits=StrTraitMFC<wchar_t>
1>        ]
1>        Constructor for class 'ATL::CStringT<BaseType,StringTraits>' is declared 'explicit'
1>        with
1>        [
1>            BaseType=wchar_t,
1>            StringTraits=StrTraitMFC<wchar_t>
1>        ]
1>e:\sip\sip_2\dialler1\dialler1\dialler1dlg.cpp(620) : error C2440: 'initializing' : cannot convert from 'LPCTSTR' to 'std::basic_string<_Elem,_Traits,_Ax>'
1>        with
1>        [
1>            _Elem=char,
1>            _Traits=std::char_traits<char>,
1>            _Ax=std::allocator<char>
1>        ]
1>        No constructor could take the source type, or constructor overload resolution was ambiguous
1>Build log was saved at "file://e:\sip\sip_2\dialler1\dialler1\Debug\BuildLog.htm"
1>dialler1 - 3 error(s), 1 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
0
 
LVL 31

Expert Comment

by:Zoppo
ID: 22994457
Hi,

IMO this can only happen if you anywhere wrote 'std::CString' - that's of course wrong. CString is not part of STL.

Maybe it would be helpful if you could post a relevant part of your code ...

ZOPPO
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 3

Author Comment

by:shaf81
ID: 22994466
output:-
Ignore above output that I earlier provided.

1>------ Build started: Project: dialler1, Configuration: Debug Win32 ------
1>Compiling...
1>dialler1Dlg.cpp
1>e:\sip\sip_2\dialler1\dialler1\dialler1dlg.cpp(619) : error C2440: 'initializing' : cannot convert from 'const char [14]' to 'ATL::CStringT<BaseType,StringTraits>'
1>        with
1>        [
1>            BaseType=wchar_t,
1>            StringTraits=StrTraitMFC<wchar_t>
1>        ]
1>        Constructor for class 'ATL::CStringT<BaseType,StringTraits>' is declared 'explicit'
1>        with
1>        [
1>            BaseType=wchar_t,
1>            StringTraits=StrTraitMFC<wchar_t>
1>        ]
1>e:\sip\sip_2\dialler1\dialler1\dialler1dlg.cpp(620) : error C2440: 'initializing' : cannot convert from 'LPCTSTR' to 'std::basic_string<_Elem,_Traits,_Ax>'
1>        with
1>        [
1>            _Elem=char,
1>            _Traits=std::char_traits<char>,
1>            _Ax=std::allocator<char>
1>        ]
1>        No constructor could take the source type, or constructor overload resolution was ambiguous
1>e:\sip\sip_2\dialler1\dialler1\dialler1dlg.cpp(1389) : warning C4244: 'argument' : conversion from 'time_t' to 'unsigned int', possible loss of data
1>Build log was saved at "file://e:\sip\sip_2\dialler1\dialler1\Debug\BuildLog.htm"
1>dialler1 - 2 error(s), 1 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

CString cStr = "Hello, world!";
	std::string str = (LPCTSTR)cStr;

Open in new window

0
 
LVL 31

Accepted Solution

by:
Zoppo earned 1000 total points
ID: 22994518
ok, I guess you use UNICODE or MBCS.

First error should be go away if you init cStr like this:

> CString cStr = _T( "Hello, world!" );

Second should go away if you use STL's wstring instead of string, i.e.:

> std::wstring str = (LPCTSTR)cStr;

Hope that helps,

ZOPPO
0
 
LVL 3

Author Comment

by:shaf81
ID: 22994602
Hi ZOPPO,

You are genious. Can you explain how could you get to that answer?
You are accurately correct.

Thank you very much !!!
0
 
LVL 3

Author Closing Comment

by:shaf81
ID: 31518216
If you explain about how would you got that  answer it will really helpful for me. Any way your answer is perfect and really helpful.
Thank you very much !!!
0
 
LVL 31

Expert Comment

by:Zoppo
ID: 22995075
Hi again,

well, my first comment based on the fact I know that 'std::string' has a constructor which takes a pointer to a char array. The LPCTSTR-cast operator of CString returns such a pointer.

As you posted the errors I found the corrections like this:

1. I know that the 'cannot convert from 'const char [14]' to 'ATL::CStringT<BaseType,StringTraits>' can only happen if the char-type used with CString isn't 'char'. The char-type used for CString is TCHAR, which is a typedef for 'WCHAR' (which is a typedef for native 'wchar_t') in unicode, otherwise TCHAR is a typedef for 'char'. So, due to experience I at once found the problem must be you use unicode and in unicode you have to declare string literals with the '_T(...)'-macro (BTW, you can use this macro even if you don't use unicode)

2. Even from my experience I know using unicode with STL requires std::wstring instead of std::string. This is due to the fact how these two are typedef'ed:
> typedef basic_string<char, char_traits<char>, allocator<char> >      string;
> typedef basic_string<wchar_t, char_traits<wchar_t>, allocator<wchar_t> > wstring;

That's all I can tell about how I found it - it's mostly just a matter of experience ;o)

Have a nice day,

best regards,

ZOPPO
0
 
LVL 3

Author Comment

by:shaf81
ID: 23001799
hi zoppo,

Thank you very much guy,  

Wish you all the best !!!

Have a nice day !!!
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
Suggested Courses

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question