Cstring convert to string

Discussion in 'C++' started by jerry, May 16, 2009.

  1. jerry

    jerry Guest

    Hi,

    How to convert from CString to stl string?

    i wrote some codes as below:

    CString Temp = "This is a test";

    string Str = Temp;

    they 's an error when i compile it, it says:
    error C2440: 'initializing' : cannot convert from 'CString' to
    'std::basic_string<_Elem,_Traits,_Ax>'

    could you give me a solution ? thankyou
    jerry, May 16, 2009
    #1
    1. Advertising

  2. * jerry:
    >
    > How to convert from CString to stl string?
    >
    > i wrote some codes as below:
    >
    > CString Temp = "This is a test";
    >
    > string Str = Temp;
    >
    > they 's an error when i compile it, it says:
    > error C2440: 'initializing' : cannot convert from 'CString' to
    > 'std::basic_string<_Elem,_Traits,_Ax>'
    >
    > could you give me a solution ? thankyou


    Check what arguments the std::string constructors require.

    Find out how you can retrieve the relevant information from a CString.

    That's it.

    By the way, note that 'CString' is a non-standard class.

    One might *guess* that it's a Microsoft MFC/ATL 'CString', but there are dozens
    of like beasts, and discussing any of them is off-topic in this group.


    Cheers & hth.,

    - Alf

    --
    Due to hosting requirements I need visits to <url: http://alfps.izfree.com/>.
    No ads, and there is some C++ stuff! :) Just going there is good. Linking
    to it is even better! Thanks in advance!
    Alf P. Steinbach, May 16, 2009
    #2
    1. Advertising

  3. jerry

    Guest

    On May 16, 10:42 am, jerry <> wrote:
    > Hi,
    >
    > How to convert from CString to stl string?
    >
    > i wrote some codes as below:
    >
    > CString Temp = "This is a test";
    >
    > string Str = Temp;
    >
    > they 's an error when i compile it, it says:
    > error C2440: 'initializing' : cannot convert from 'CString' to
    > 'std::basic_string<_Elem,_Traits,_Ax>'
    >
    > could you give me a solution ? thankyou


    Most third party "string" implementations allow you to get at a
    pointer that points to a null terminated C-style string. Some may not
    be null terminated but return the pointer plus a length. From there,
    you can construct a std::string. Just look up the various
    constructors for std::string.

    HTH
    , May 16, 2009
    #3
  4. jerry

    jerry Guest

    On May 16, 10:46 pm, "Alf P. Steinbach" <> wrote:
    > * jerry:
    >
    >
    >
    > > How to convert from CString to stl string?

    >
    > > i wrote some codes as below:

    >
    > > CString Temp = "This is a test";

    >
    > > string Str = Temp;

    >
    > > they 's an error when i compile it, it says:
    > > error C2440: 'initializing' : cannot convert from 'CString' to
    > > 'std::basic_string<_Elem,_Traits,_Ax>'

    >
    > > could you give me a solution ? thankyou

    >
    > Check what arguments the std::string constructors require.
    >
    > Find out how you can retrieve the relevant information from a CString.
    >
    > That's it.
    >
    > By the way, note that 'CString' is a non-standard class.
    >
    > One might *guess* that it's a Microsoft MFC/ATL 'CString', but there are dozens
    > of like beasts, and discussing any of them is off-topic in this group.
    >
    > Cheers & hth.,
    >
    > - Alf
    >
    > --
    > Due to hosting requirements I need visits to <url:http://alfps.izfree.com/>.
    > No ads, and there is some C++ stuff! :) Just going there is good. Linking
    > to it is even better! Thanks in advance!



    YOU SAID IT, CString is mfc class;
    jerry, May 17, 2009
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Markus Hämmerli
    Replies:
    6
    Views:
    7,749
    Peter van Merkerk
    Sep 4, 2003
  2. jt
    Replies:
    2
    Views:
    862
    upashu2
    Jul 14, 2005
  3. michael mazanek

    convert VARIANT to CSTRING

    michael mazanek, Sep 18, 2006, in forum: C++
    Replies:
    1
    Views:
    696
    Howard
    Sep 18, 2006
  4. Gertjan Huiskes

    Convert CString to Variant

    Gertjan Huiskes, Jan 4, 2007, in forum: C++
    Replies:
    1
    Views:
    613
    Victor Bazarov
    Jan 4, 2007
  5. 月夕

    How to convert CString to LPCWSTR

    月夕, Jul 5, 2009, in forum: C++
    Replies:
    3
    Views:
    1,983
    Jorgen Grahn
    Jul 6, 2009
Loading...

Share This Page