Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. error in CString to const std::string conversion.

error in CString to const std::string conversion.

Scheduled Pinned Locked Moved C / C++ / MFC
help
9 Posts 6 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    Le rner
    wrote on last edited by
    #1

    Hi all, i have an problem to convert a CString value in const std::string.

    CString result="";
    CString str="test";

    const std::string s = str;//here gives an error
    

    /*
    error C2440: 'initializing' : cannot convert from 'CString' to 'std::basic_string<_Elem,_Traits,_Ax>'
    */

    std::string s1 = fun1(s);
    
    result=s1.c\_str();
    

    please help me for this. thanks in advance.

    S R P 3 Replies Last reply
    0
    • L Le rner

      Hi all, i have an problem to convert a CString value in const std::string.

      CString result="";
      CString str="test";

      const std::string s = str;//here gives an error
      

      /*
      error C2440: 'initializing' : cannot convert from 'CString' to 'std::basic_string<_Elem,_Traits,_Ax>'
      */

      std::string s1 = fun1(s);
      
      result=s1.c\_str();
      

      please help me for this. thanks in advance.

      S Offline
      S Offline
      ShilpiP
      wrote on last edited by
      #2

      How to: Convert Between Various String Types ->[^]

      "Every Little Smile can touch Somebody's Heart... May we find Hundreds of Reasons to Smile Everyday... and May WE be the Reason for someone else to smile always!" (ICAN) "Your thoughts are the architects of your destiny."

      L 1 Reply Last reply
      0
      • S ShilpiP

        How to: Convert Between Various String Types ->[^]

        "Every Little Smile can touch Somebody's Heart... May we find Hundreds of Reasons to Smile Everyday... and May WE be the Reason for someone else to smile always!" (ICAN) "Your thoughts are the architects of your destiny."

        L Offline
        L Offline
        Le rner
        wrote on last edited by
        #3

        thanks

        1 Reply Last reply
        0
        • L Le rner

          Hi all, i have an problem to convert a CString value in const std::string.

          CString result="";
          CString str="test";

          const std::string s = str;//here gives an error
          

          /*
          error C2440: 'initializing' : cannot convert from 'CString' to 'std::basic_string<_Elem,_Traits,_Ax>'
          */

          std::string s1 = fun1(s);
          
          result=s1.c\_str();
          

          please help me for this. thanks in advance.

          R Offline
          R Offline
          Rajesh R Subramanian
          wrote on last edited by
          #4

          Are you building for Unicode or MBCS? I think you're building for Unicode, but are using the ANSI version of std string. Try the following:

          //Unicode build
          CString sz = L"Hello";
          const std::wstring str = sz;

          //MBCS build
          CStringA sz = "Hello";
          const std::string str = sz;

          "Real men drive manual transmission" - Rajesh.

          L 1 Reply Last reply
          0
          • R Rajesh R Subramanian

            Are you building for Unicode or MBCS? I think you're building for Unicode, but are using the ANSI version of std string. Try the following:

            //Unicode build
            CString sz = L"Hello";
            const std::wstring str = sz;

            //MBCS build
            CStringA sz = "Hello";
            const std::string str = sz;

            "Real men drive manual transmission" - Rajesh.

            L Offline
            L Offline
            Le rner
            wrote on last edited by
            #5

            thanks but now one more error is generated

            'fun1' : cannot convert parameter 1 from 'const std::wstring' to 'const std::string &'

            and fun1 is like this

            std::string fun1 (std::string const& s);

            L 1 Reply Last reply
            0
            • L Le rner

              thanks but now one more error is generated

              'fun1' : cannot convert parameter 1 from 'const std::wstring' to 'const std::string &'

              and fun1 is like this

              std::string fun1 (std::string const& s);

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #6

              You need to be consistent in your class selections, use all unicode or all MBCS. If you are using Unicode then you need to use the std::wstring class rather than string.

              The best things in life are not things.

              P 1 Reply Last reply
              0
              • L Lost User

                You need to be consistent in your class selections, use all unicode or all MBCS. If you are using Unicode then you need to use the std::wstring class rather than string.

                The best things in life are not things.

                P Offline
                P Offline
                PJ Arends
                wrote on last edited by
                #7

                Or use std::tstring[^] and not have to worry about it at all.

                Independent ACN representative

                -- Check out the possibilities for your future!

                -- Financial independance

                -- Full time or Part time

                -- In more than 20 countries through North America, Europe, Asia and the Pacific


                Within you lies the power for good - Use it!

                L 1 Reply Last reply
                0
                • P PJ Arends

                  Or use std::tstring[^] and not have to worry about it at all.

                  Independent ACN representative

                  -- Check out the possibilities for your future!

                  -- Financial independance

                  -- Full time or Part time

                  -- In more than 20 countries through North America, Europe, Asia and the Pacific


                  Within you lies the power for good - Use it!

                  L Offline
                  L Offline
                  Lost User
                  wrote on last edited by
                  #8

                  PJ Arends wrote:

                  Or use std::tstring[^] and not have to worry about it at all.

                  A simple typedef, something like

                  #if defined(UNICODE)
                  typedef std::wstring STRING
                  #else
                  typedef std::string STRING
                  #endif

                  would also do it, since both are implementations of basic_string and its functions.

                  The best things in life are not things.

                  1 Reply Last reply
                  0
                  • L Le rner

                    Hi all, i have an problem to convert a CString value in const std::string.

                    CString result="";
                    CString str="test";

                    const std::string s = str;//here gives an error
                    

                    /*
                    error C2440: 'initializing' : cannot convert from 'CString' to 'std::basic_string<_Elem,_Traits,_Ax>'
                    */

                    std::string s1 = fun1(s);
                    
                    result=s1.c\_str();
                    

                    please help me for this. thanks in advance.

                    P Offline
                    P Offline
                    pratik_mishra35
                    wrote on last edited by
                    #9

                    Include the "atlconv.h". Then use one of the macros defined in this article http://msdn.microsoft.com/en-us/library/87zae4a3%28v=vs.80%29.aspx[^]

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups