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 while converting int to string...

Error while converting int to string...

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestion
5 Posts 5 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.
  • P Offline
    P Offline
    pl_kode
    wrote on last edited by
    #1

    I am trying to convert an int to string and get an error. I have the following...

    string str="1";
    int val;
    val=atoi(str.c_str()); // Converts String to INT
    val++;
    itoa(val,str,10);//Converting INT to string

    On doing this I get the following error. Were could I have gone wrong?

    error C2664: 'itoa' : cannot convert parameter 2 from 'class std::basic_string,class std::allocator >' to 'char *'
    No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called

    THANKS.

    _ R C 3 Replies Last reply
    0
    • P pl_kode

      I am trying to convert an int to string and get an error. I have the following...

      string str="1";
      int val;
      val=atoi(str.c_str()); // Converts String to INT
      val++;
      itoa(val,str,10);//Converting INT to string

      On doing this I get the following error. Were could I have gone wrong?

      error C2664: 'itoa' : cannot convert parameter 2 from 'class std::basic_string,class std::allocator >' to 'char *'
      No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called

      THANKS.

      _ Offline
      _ Offline
      _AnsHUMAN_
      wrote on last edited by
      #2
      string str="1";
      int val;
      val=atoi(str.c_str()); 
      // Converts String to INT
      val++;
      itoa(val,(char*)str.c_str(),10);//Converting INT to string
      

      The code would serve your purpose but not be the optimized way of doing this.

      Somethings seem HARD to do, until we know how to do them. ;-)_AnShUmAn_

      S 1 Reply Last reply
      0
      • P pl_kode

        I am trying to convert an int to string and get an error. I have the following...

        string str="1";
        int val;
        val=atoi(str.c_str()); // Converts String to INT
        val++;
        itoa(val,str,10);//Converting INT to string

        On doing this I get the following error. Were could I have gone wrong?

        error C2664: 'itoa' : cannot convert parameter 2 from 'class std::basic_string,class std::allocator >' to 'char *'
        No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called

        THANKS.

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

        See the answers given to the following queries: Asked on 12th May 2008[^] Asked on 13th May 2008[^] Asked on 14th May 2008[^] You forgot to ask it on 15th May 2008. Today is 16th May 2008 and you DID ask the same thing.

        Nobody can give you wiser advice than yourself. - Cicero .·´¯`·->Rajesh<-·´¯`·. Codeproject.com: Visual C++ MVP

        1 Reply Last reply
        0
        • P pl_kode

          I am trying to convert an int to string and get an error. I have the following...

          string str="1";
          int val;
          val=atoi(str.c_str()); // Converts String to INT
          val++;
          itoa(val,str,10);//Converting INT to string

          On doing this I get the following error. Were could I have gone wrong?

          error C2664: 'itoa' : cannot convert parameter 2 from 'class std::basic_string,class std::allocator >' to 'char *'
          No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called

          THANKS.

          C Offline
          C Offline
          CPallini
          wrote on last edited by
          #4

          string s= "1";
          strstream str;
          int n;

          str << s;
          str >> n;
          n++;
          str.clear();
          str << n << '\0';
          s = str.str();

          :)

          If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
          This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke

          1 Reply Last reply
          0
          • _ _AnsHUMAN_
            string str="1";
            int val;
            val=atoi(str.c_str()); 
            // Converts String to INT
            val++;
            itoa(val,(char*)str.c_str(),10);//Converting INT to string
            

            The code would serve your purpose but not be the optimized way of doing this.

            Somethings seem HARD to do, until we know how to do them. ;-)_AnShUmAn_

            S Offline
            S Offline
            Stephen Hewitt
            wrote on last edited by
            #5

            _AnShUmAn_ wrote:

            (char*)str.c_str(

            This is a bad idea. The return type is const char * and casting away the const is wrong and the compiler is correct in complaining.

            Steve

            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