I'm getting a compile time error when appending a string to a const char* in C++?












0














so I'm trying to convert an integer value into a string and then append the string to an already existing const char*. But as mentioned, I'm getting this error:




error: expression must have integral or unscoped enum type.



 objDim += objDimStr.c_str();



Any ideas why?
My code is as follows:



    const char* objDim = "The object dimension is: ";
int objDimension = geo->Dimension();
std::string objDimStr = std::to_string(objDimension);
objDim += objDimStr.c_str();









share|improve this question




















  • 2




    They're addresses, so that's like adding 1428, Elm Street to 946, Elderberry Road and expecting Elderberry Road to attach to Elm Street.
    – molbdnilo
    Nov 20 at 12:34


















0














so I'm trying to convert an integer value into a string and then append the string to an already existing const char*. But as mentioned, I'm getting this error:




error: expression must have integral or unscoped enum type.



 objDim += objDimStr.c_str();



Any ideas why?
My code is as follows:



    const char* objDim = "The object dimension is: ";
int objDimension = geo->Dimension();
std::string objDimStr = std::to_string(objDimension);
objDim += objDimStr.c_str();









share|improve this question




















  • 2




    They're addresses, so that's like adding 1428, Elm Street to 946, Elderberry Road and expecting Elderberry Road to attach to Elm Street.
    – molbdnilo
    Nov 20 at 12:34
















0












0








0







so I'm trying to convert an integer value into a string and then append the string to an already existing const char*. But as mentioned, I'm getting this error:




error: expression must have integral or unscoped enum type.



 objDim += objDimStr.c_str();



Any ideas why?
My code is as follows:



    const char* objDim = "The object dimension is: ";
int objDimension = geo->Dimension();
std::string objDimStr = std::to_string(objDimension);
objDim += objDimStr.c_str();









share|improve this question















so I'm trying to convert an integer value into a string and then append the string to an already existing const char*. But as mentioned, I'm getting this error:




error: expression must have integral or unscoped enum type.



 objDim += objDimStr.c_str();



Any ideas why?
My code is as follows:



    const char* objDim = "The object dimension is: ";
int objDimension = geo->Dimension();
std::string objDimStr = std::to_string(objDimension);
objDim += objDimStr.c_str();






c++ string char






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 20 at 13:48

























asked Nov 20 at 12:27









Xyber 101

33




33








  • 2




    They're addresses, so that's like adding 1428, Elm Street to 946, Elderberry Road and expecting Elderberry Road to attach to Elm Street.
    – molbdnilo
    Nov 20 at 12:34
















  • 2




    They're addresses, so that's like adding 1428, Elm Street to 946, Elderberry Road and expecting Elderberry Road to attach to Elm Street.
    – molbdnilo
    Nov 20 at 12:34










2




2




They're addresses, so that's like adding 1428, Elm Street to 946, Elderberry Road and expecting Elderberry Road to attach to Elm Street.
– molbdnilo
Nov 20 at 12:34






They're addresses, so that's like adding 1428, Elm Street to 946, Elderberry Road and expecting Elderberry Road to attach to Elm Street.
– molbdnilo
Nov 20 at 12:34














5 Answers
5






active

oldest

votes


















2














objDim is a pointer. When you add to the pointer, you will add to the pointer itself and not append anything to the data it points to.



And you can not add everything to a pointer, basically only integer values. And adding two pointers like you do makes no sense at all (which is why it's not allowed).



If you want to be able to append strings, then use std::string for all strings.



Besides, that you need to use the const qualifier should also give some hints about that what the pointer is pointing to is constant and therefore can't be modified (including added to).





And in this case you don't need any of the temporary variable objDimension or objDimStr (unless you use them later in the code):



std::string objDim = "The object dimension is: " + std::to_string(geo->Dimension());


Here the addition works because there is an overloaded operator+ function that takes the correct arguments in the correct order, and returns a new std::string object.






share|improve this answer























  • Okay, but after doing that I will be left with a string. But, I need a const char* type variable to be able to pass it to an API function to display the string on the screen. The function signature is : int RhinoMessageBox ( const char * message, const char * title, UINT type ). So, how do I get a string and then pass it into the function suitably?
    – Xyber 101
    Nov 20 at 12:56










  • @Xyber101 Well you already seem to know about the c_str() member function...
    – Some programmer dude
    Nov 20 at 13:00










  • Uhh yeah, that was so simple. I guess I should go through my code first and stop coming here and posting the error. Thanks !
    – Xyber 101
    Nov 20 at 13:11






  • 1




    @Xyber101 No worries. Sometimes it's much to easy to forget such small simple details. :)
    – Some programmer dude
    Nov 20 at 13:14










  • A pointer points to constant chars. The pointer itself can be modified. If concatenation was a standard operation in C++, chances are, one could add more characters past the otherwise constant string.
    – bipll
    Nov 20 at 14:36



















2














I assume you were expecting to get a concatenation of strings. No, it's not how it works. objDim is but a pointer to a non-modifiable memory area that contains characters. The only possible application of operator += to it that would compile is if you were incrementing it by a few characters:



objDim += 11;
std::cout << objDim << 'n'; // prints "dimension is: ";


To concatenate strings you can:





  1. Either assign the result to a string object:



    std::string result = objDim + objDimStr;


    (Note the absence of c_str anywhere: you cannot sum two pointers, but there's an overload that can prepend a char pointer to a std::string.)




  2. Or give proper type to objDim;



    std::string objDim{"The object dimension is: "};
    objDim += objDimStr;







share|improve this answer





























    1














    objDim is not an std::string but a pointer, so your concatenation operator (+) which is defined for std::string will not work on it.



    Define it as an std::string and you will be able to use it to concatenate other std::strings to it.






    share|improve this answer





















    • Okay, I perfectly get what you are saying, a string will be appended with a string only, not a pointer. But, I need a char* variable, as that is what I have to pass to an API function to display the string on the screen. The function signature is : int RhinoMessageBox ( const char * message, const char * title, UINT type ) ?
      – Xyber 101
      Nov 20 at 12:52












    • @Xyber101: Then you can do objDimStr += objDim + objDimStr;. Then objDimStr will contain what you need in the right order.
      – P.W
      Nov 20 at 12:57












    • Again, that would give me a string wouldn't it? What I need is a char* with the data of the string. How do I do it? Sorry, I am a newbie in C++ and this pointer thing has got me pretty tied up :(
      – Xyber 101
      Nov 20 at 13:01










    • @Xyber101: objDimStr.c_str() will give you what you want. It is the C string equivalent (const char*)
      – P.W
      Nov 20 at 13:03










    • Yeah, thanks :) Thank you so much for your time, P.W !
      – Xyber 101
      Nov 20 at 13:12



















    0














    objDim += objDimStr.c_str();


    Here you add one pointer to other instead of concatenating the string. You need use std::string to concatenate, for example:



    const char* objDim = "The object dimension is: ";
    int objDimension = geo->Dimension();
    std::string objDimStr = objDim;
    objDimStr += std::to_string(objDimension);
    std::cout << objDimStr << std::endl;





    share|improve this answer





























      -1














      You're declaring objDim as a const, which means it can NOT be changed in runtime.



      You should either use std::string instead of const char* or use strncat, as seen here: Append to the end of a Char array in C++






      share|improve this answer





















        Your Answer






        StackExchange.ifUsing("editor", function () {
        StackExchange.using("externalEditor", function () {
        StackExchange.using("snippets", function () {
        StackExchange.snippets.init();
        });
        });
        }, "code-snippets");

        StackExchange.ready(function() {
        var channelOptions = {
        tags: "".split(" "),
        id: "1"
        };
        initTagRenderer("".split(" "), "".split(" "), channelOptions);

        StackExchange.using("externalEditor", function() {
        // Have to fire editor after snippets, if snippets enabled
        if (StackExchange.settings.snippets.snippetsEnabled) {
        StackExchange.using("snippets", function() {
        createEditor();
        });
        }
        else {
        createEditor();
        }
        });

        function createEditor() {
        StackExchange.prepareEditor({
        heartbeatType: 'answer',
        autoActivateHeartbeat: false,
        convertImagesToLinks: true,
        noModals: true,
        showLowRepImageUploadWarning: true,
        reputationToPostImages: 10,
        bindNavPrevention: true,
        postfix: "",
        imageUploader: {
        brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
        contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
        allowUrls: true
        },
        onDemand: true,
        discardSelector: ".discard-answer"
        ,immediatelyShowMarkdownHelp:true
        });


        }
        });














        draft saved

        draft discarded


















        StackExchange.ready(
        function () {
        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53393000%2fim-getting-a-compile-time-error-when-appending-a-string-to-a-const-char-in-c%23new-answer', 'question_page');
        }
        );

        Post as a guest















        Required, but never shown

























        5 Answers
        5






        active

        oldest

        votes








        5 Answers
        5






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes









        2














        objDim is a pointer. When you add to the pointer, you will add to the pointer itself and not append anything to the data it points to.



        And you can not add everything to a pointer, basically only integer values. And adding two pointers like you do makes no sense at all (which is why it's not allowed).



        If you want to be able to append strings, then use std::string for all strings.



        Besides, that you need to use the const qualifier should also give some hints about that what the pointer is pointing to is constant and therefore can't be modified (including added to).





        And in this case you don't need any of the temporary variable objDimension or objDimStr (unless you use them later in the code):



        std::string objDim = "The object dimension is: " + std::to_string(geo->Dimension());


        Here the addition works because there is an overloaded operator+ function that takes the correct arguments in the correct order, and returns a new std::string object.






        share|improve this answer























        • Okay, but after doing that I will be left with a string. But, I need a const char* type variable to be able to pass it to an API function to display the string on the screen. The function signature is : int RhinoMessageBox ( const char * message, const char * title, UINT type ). So, how do I get a string and then pass it into the function suitably?
          – Xyber 101
          Nov 20 at 12:56










        • @Xyber101 Well you already seem to know about the c_str() member function...
          – Some programmer dude
          Nov 20 at 13:00










        • Uhh yeah, that was so simple. I guess I should go through my code first and stop coming here and posting the error. Thanks !
          – Xyber 101
          Nov 20 at 13:11






        • 1




          @Xyber101 No worries. Sometimes it's much to easy to forget such small simple details. :)
          – Some programmer dude
          Nov 20 at 13:14










        • A pointer points to constant chars. The pointer itself can be modified. If concatenation was a standard operation in C++, chances are, one could add more characters past the otherwise constant string.
          – bipll
          Nov 20 at 14:36
















        2














        objDim is a pointer. When you add to the pointer, you will add to the pointer itself and not append anything to the data it points to.



        And you can not add everything to a pointer, basically only integer values. And adding two pointers like you do makes no sense at all (which is why it's not allowed).



        If you want to be able to append strings, then use std::string for all strings.



        Besides, that you need to use the const qualifier should also give some hints about that what the pointer is pointing to is constant and therefore can't be modified (including added to).





        And in this case you don't need any of the temporary variable objDimension or objDimStr (unless you use them later in the code):



        std::string objDim = "The object dimension is: " + std::to_string(geo->Dimension());


        Here the addition works because there is an overloaded operator+ function that takes the correct arguments in the correct order, and returns a new std::string object.






        share|improve this answer























        • Okay, but after doing that I will be left with a string. But, I need a const char* type variable to be able to pass it to an API function to display the string on the screen. The function signature is : int RhinoMessageBox ( const char * message, const char * title, UINT type ). So, how do I get a string and then pass it into the function suitably?
          – Xyber 101
          Nov 20 at 12:56










        • @Xyber101 Well you already seem to know about the c_str() member function...
          – Some programmer dude
          Nov 20 at 13:00










        • Uhh yeah, that was so simple. I guess I should go through my code first and stop coming here and posting the error. Thanks !
          – Xyber 101
          Nov 20 at 13:11






        • 1




          @Xyber101 No worries. Sometimes it's much to easy to forget such small simple details. :)
          – Some programmer dude
          Nov 20 at 13:14










        • A pointer points to constant chars. The pointer itself can be modified. If concatenation was a standard operation in C++, chances are, one could add more characters past the otherwise constant string.
          – bipll
          Nov 20 at 14:36














        2












        2








        2






        objDim is a pointer. When you add to the pointer, you will add to the pointer itself and not append anything to the data it points to.



        And you can not add everything to a pointer, basically only integer values. And adding two pointers like you do makes no sense at all (which is why it's not allowed).



        If you want to be able to append strings, then use std::string for all strings.



        Besides, that you need to use the const qualifier should also give some hints about that what the pointer is pointing to is constant and therefore can't be modified (including added to).





        And in this case you don't need any of the temporary variable objDimension or objDimStr (unless you use them later in the code):



        std::string objDim = "The object dimension is: " + std::to_string(geo->Dimension());


        Here the addition works because there is an overloaded operator+ function that takes the correct arguments in the correct order, and returns a new std::string object.






        share|improve this answer














        objDim is a pointer. When you add to the pointer, you will add to the pointer itself and not append anything to the data it points to.



        And you can not add everything to a pointer, basically only integer values. And adding two pointers like you do makes no sense at all (which is why it's not allowed).



        If you want to be able to append strings, then use std::string for all strings.



        Besides, that you need to use the const qualifier should also give some hints about that what the pointer is pointing to is constant and therefore can't be modified (including added to).





        And in this case you don't need any of the temporary variable objDimension or objDimStr (unless you use them later in the code):



        std::string objDim = "The object dimension is: " + std::to_string(geo->Dimension());


        Here the addition works because there is an overloaded operator+ function that takes the correct arguments in the correct order, and returns a new std::string object.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 20 at 12:42

























        answered Nov 20 at 12:31









        Some programmer dude

        293k24247408




        293k24247408












        • Okay, but after doing that I will be left with a string. But, I need a const char* type variable to be able to pass it to an API function to display the string on the screen. The function signature is : int RhinoMessageBox ( const char * message, const char * title, UINT type ). So, how do I get a string and then pass it into the function suitably?
          – Xyber 101
          Nov 20 at 12:56










        • @Xyber101 Well you already seem to know about the c_str() member function...
          – Some programmer dude
          Nov 20 at 13:00










        • Uhh yeah, that was so simple. I guess I should go through my code first and stop coming here and posting the error. Thanks !
          – Xyber 101
          Nov 20 at 13:11






        • 1




          @Xyber101 No worries. Sometimes it's much to easy to forget such small simple details. :)
          – Some programmer dude
          Nov 20 at 13:14










        • A pointer points to constant chars. The pointer itself can be modified. If concatenation was a standard operation in C++, chances are, one could add more characters past the otherwise constant string.
          – bipll
          Nov 20 at 14:36


















        • Okay, but after doing that I will be left with a string. But, I need a const char* type variable to be able to pass it to an API function to display the string on the screen. The function signature is : int RhinoMessageBox ( const char * message, const char * title, UINT type ). So, how do I get a string and then pass it into the function suitably?
          – Xyber 101
          Nov 20 at 12:56










        • @Xyber101 Well you already seem to know about the c_str() member function...
          – Some programmer dude
          Nov 20 at 13:00










        • Uhh yeah, that was so simple. I guess I should go through my code first and stop coming here and posting the error. Thanks !
          – Xyber 101
          Nov 20 at 13:11






        • 1




          @Xyber101 No worries. Sometimes it's much to easy to forget such small simple details. :)
          – Some programmer dude
          Nov 20 at 13:14










        • A pointer points to constant chars. The pointer itself can be modified. If concatenation was a standard operation in C++, chances are, one could add more characters past the otherwise constant string.
          – bipll
          Nov 20 at 14:36
















        Okay, but after doing that I will be left with a string. But, I need a const char* type variable to be able to pass it to an API function to display the string on the screen. The function signature is : int RhinoMessageBox ( const char * message, const char * title, UINT type ). So, how do I get a string and then pass it into the function suitably?
        – Xyber 101
        Nov 20 at 12:56




        Okay, but after doing that I will be left with a string. But, I need a const char* type variable to be able to pass it to an API function to display the string on the screen. The function signature is : int RhinoMessageBox ( const char * message, const char * title, UINT type ). So, how do I get a string and then pass it into the function suitably?
        – Xyber 101
        Nov 20 at 12:56












        @Xyber101 Well you already seem to know about the c_str() member function...
        – Some programmer dude
        Nov 20 at 13:00




        @Xyber101 Well you already seem to know about the c_str() member function...
        – Some programmer dude
        Nov 20 at 13:00












        Uhh yeah, that was so simple. I guess I should go through my code first and stop coming here and posting the error. Thanks !
        – Xyber 101
        Nov 20 at 13:11




        Uhh yeah, that was so simple. I guess I should go through my code first and stop coming here and posting the error. Thanks !
        – Xyber 101
        Nov 20 at 13:11




        1




        1




        @Xyber101 No worries. Sometimes it's much to easy to forget such small simple details. :)
        – Some programmer dude
        Nov 20 at 13:14




        @Xyber101 No worries. Sometimes it's much to easy to forget such small simple details. :)
        – Some programmer dude
        Nov 20 at 13:14












        A pointer points to constant chars. The pointer itself can be modified. If concatenation was a standard operation in C++, chances are, one could add more characters past the otherwise constant string.
        – bipll
        Nov 20 at 14:36




        A pointer points to constant chars. The pointer itself can be modified. If concatenation was a standard operation in C++, chances are, one could add more characters past the otherwise constant string.
        – bipll
        Nov 20 at 14:36













        2














        I assume you were expecting to get a concatenation of strings. No, it's not how it works. objDim is but a pointer to a non-modifiable memory area that contains characters. The only possible application of operator += to it that would compile is if you were incrementing it by a few characters:



        objDim += 11;
        std::cout << objDim << 'n'; // prints "dimension is: ";


        To concatenate strings you can:





        1. Either assign the result to a string object:



          std::string result = objDim + objDimStr;


          (Note the absence of c_str anywhere: you cannot sum two pointers, but there's an overload that can prepend a char pointer to a std::string.)




        2. Or give proper type to objDim;



          std::string objDim{"The object dimension is: "};
          objDim += objDimStr;







        share|improve this answer


























          2














          I assume you were expecting to get a concatenation of strings. No, it's not how it works. objDim is but a pointer to a non-modifiable memory area that contains characters. The only possible application of operator += to it that would compile is if you were incrementing it by a few characters:



          objDim += 11;
          std::cout << objDim << 'n'; // prints "dimension is: ";


          To concatenate strings you can:





          1. Either assign the result to a string object:



            std::string result = objDim + objDimStr;


            (Note the absence of c_str anywhere: you cannot sum two pointers, but there's an overload that can prepend a char pointer to a std::string.)




          2. Or give proper type to objDim;



            std::string objDim{"The object dimension is: "};
            objDim += objDimStr;







          share|improve this answer
























            2












            2








            2






            I assume you were expecting to get a concatenation of strings. No, it's not how it works. objDim is but a pointer to a non-modifiable memory area that contains characters. The only possible application of operator += to it that would compile is if you were incrementing it by a few characters:



            objDim += 11;
            std::cout << objDim << 'n'; // prints "dimension is: ";


            To concatenate strings you can:





            1. Either assign the result to a string object:



              std::string result = objDim + objDimStr;


              (Note the absence of c_str anywhere: you cannot sum two pointers, but there's an overload that can prepend a char pointer to a std::string.)




            2. Or give proper type to objDim;



              std::string objDim{"The object dimension is: "};
              objDim += objDimStr;







            share|improve this answer












            I assume you were expecting to get a concatenation of strings. No, it's not how it works. objDim is but a pointer to a non-modifiable memory area that contains characters. The only possible application of operator += to it that would compile is if you were incrementing it by a few characters:



            objDim += 11;
            std::cout << objDim << 'n'; // prints "dimension is: ";


            To concatenate strings you can:





            1. Either assign the result to a string object:



              std::string result = objDim + objDimStr;


              (Note the absence of c_str anywhere: you cannot sum two pointers, but there's an overload that can prepend a char pointer to a std::string.)




            2. Or give proper type to objDim;



              std::string objDim{"The object dimension is: "};
              objDim += objDimStr;








            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 20 at 12:35









            bipll

            7,9321825




            7,9321825























                1














                objDim is not an std::string but a pointer, so your concatenation operator (+) which is defined for std::string will not work on it.



                Define it as an std::string and you will be able to use it to concatenate other std::strings to it.






                share|improve this answer





















                • Okay, I perfectly get what you are saying, a string will be appended with a string only, not a pointer. But, I need a char* variable, as that is what I have to pass to an API function to display the string on the screen. The function signature is : int RhinoMessageBox ( const char * message, const char * title, UINT type ) ?
                  – Xyber 101
                  Nov 20 at 12:52












                • @Xyber101: Then you can do objDimStr += objDim + objDimStr;. Then objDimStr will contain what you need in the right order.
                  – P.W
                  Nov 20 at 12:57












                • Again, that would give me a string wouldn't it? What I need is a char* with the data of the string. How do I do it? Sorry, I am a newbie in C++ and this pointer thing has got me pretty tied up :(
                  – Xyber 101
                  Nov 20 at 13:01










                • @Xyber101: objDimStr.c_str() will give you what you want. It is the C string equivalent (const char*)
                  – P.W
                  Nov 20 at 13:03










                • Yeah, thanks :) Thank you so much for your time, P.W !
                  – Xyber 101
                  Nov 20 at 13:12
















                1














                objDim is not an std::string but a pointer, so your concatenation operator (+) which is defined for std::string will not work on it.



                Define it as an std::string and you will be able to use it to concatenate other std::strings to it.






                share|improve this answer





















                • Okay, I perfectly get what you are saying, a string will be appended with a string only, not a pointer. But, I need a char* variable, as that is what I have to pass to an API function to display the string on the screen. The function signature is : int RhinoMessageBox ( const char * message, const char * title, UINT type ) ?
                  – Xyber 101
                  Nov 20 at 12:52












                • @Xyber101: Then you can do objDimStr += objDim + objDimStr;. Then objDimStr will contain what you need in the right order.
                  – P.W
                  Nov 20 at 12:57












                • Again, that would give me a string wouldn't it? What I need is a char* with the data of the string. How do I do it? Sorry, I am a newbie in C++ and this pointer thing has got me pretty tied up :(
                  – Xyber 101
                  Nov 20 at 13:01










                • @Xyber101: objDimStr.c_str() will give you what you want. It is the C string equivalent (const char*)
                  – P.W
                  Nov 20 at 13:03










                • Yeah, thanks :) Thank you so much for your time, P.W !
                  – Xyber 101
                  Nov 20 at 13:12














                1












                1








                1






                objDim is not an std::string but a pointer, so your concatenation operator (+) which is defined for std::string will not work on it.



                Define it as an std::string and you will be able to use it to concatenate other std::strings to it.






                share|improve this answer












                objDim is not an std::string but a pointer, so your concatenation operator (+) which is defined for std::string will not work on it.



                Define it as an std::string and you will be able to use it to concatenate other std::strings to it.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 20 at 12:33









                P.W

                10.9k3742




                10.9k3742












                • Okay, I perfectly get what you are saying, a string will be appended with a string only, not a pointer. But, I need a char* variable, as that is what I have to pass to an API function to display the string on the screen. The function signature is : int RhinoMessageBox ( const char * message, const char * title, UINT type ) ?
                  – Xyber 101
                  Nov 20 at 12:52












                • @Xyber101: Then you can do objDimStr += objDim + objDimStr;. Then objDimStr will contain what you need in the right order.
                  – P.W
                  Nov 20 at 12:57












                • Again, that would give me a string wouldn't it? What I need is a char* with the data of the string. How do I do it? Sorry, I am a newbie in C++ and this pointer thing has got me pretty tied up :(
                  – Xyber 101
                  Nov 20 at 13:01










                • @Xyber101: objDimStr.c_str() will give you what you want. It is the C string equivalent (const char*)
                  – P.W
                  Nov 20 at 13:03










                • Yeah, thanks :) Thank you so much for your time, P.W !
                  – Xyber 101
                  Nov 20 at 13:12


















                • Okay, I perfectly get what you are saying, a string will be appended with a string only, not a pointer. But, I need a char* variable, as that is what I have to pass to an API function to display the string on the screen. The function signature is : int RhinoMessageBox ( const char * message, const char * title, UINT type ) ?
                  – Xyber 101
                  Nov 20 at 12:52












                • @Xyber101: Then you can do objDimStr += objDim + objDimStr;. Then objDimStr will contain what you need in the right order.
                  – P.W
                  Nov 20 at 12:57












                • Again, that would give me a string wouldn't it? What I need is a char* with the data of the string. How do I do it? Sorry, I am a newbie in C++ and this pointer thing has got me pretty tied up :(
                  – Xyber 101
                  Nov 20 at 13:01










                • @Xyber101: objDimStr.c_str() will give you what you want. It is the C string equivalent (const char*)
                  – P.W
                  Nov 20 at 13:03










                • Yeah, thanks :) Thank you so much for your time, P.W !
                  – Xyber 101
                  Nov 20 at 13:12
















                Okay, I perfectly get what you are saying, a string will be appended with a string only, not a pointer. But, I need a char* variable, as that is what I have to pass to an API function to display the string on the screen. The function signature is : int RhinoMessageBox ( const char * message, const char * title, UINT type ) ?
                – Xyber 101
                Nov 20 at 12:52






                Okay, I perfectly get what you are saying, a string will be appended with a string only, not a pointer. But, I need a char* variable, as that is what I have to pass to an API function to display the string on the screen. The function signature is : int RhinoMessageBox ( const char * message, const char * title, UINT type ) ?
                – Xyber 101
                Nov 20 at 12:52














                @Xyber101: Then you can do objDimStr += objDim + objDimStr;. Then objDimStr will contain what you need in the right order.
                – P.W
                Nov 20 at 12:57






                @Xyber101: Then you can do objDimStr += objDim + objDimStr;. Then objDimStr will contain what you need in the right order.
                – P.W
                Nov 20 at 12:57














                Again, that would give me a string wouldn't it? What I need is a char* with the data of the string. How do I do it? Sorry, I am a newbie in C++ and this pointer thing has got me pretty tied up :(
                – Xyber 101
                Nov 20 at 13:01




                Again, that would give me a string wouldn't it? What I need is a char* with the data of the string. How do I do it? Sorry, I am a newbie in C++ and this pointer thing has got me pretty tied up :(
                – Xyber 101
                Nov 20 at 13:01












                @Xyber101: objDimStr.c_str() will give you what you want. It is the C string equivalent (const char*)
                – P.W
                Nov 20 at 13:03




                @Xyber101: objDimStr.c_str() will give you what you want. It is the C string equivalent (const char*)
                – P.W
                Nov 20 at 13:03












                Yeah, thanks :) Thank you so much for your time, P.W !
                – Xyber 101
                Nov 20 at 13:12




                Yeah, thanks :) Thank you so much for your time, P.W !
                – Xyber 101
                Nov 20 at 13:12











                0














                objDim += objDimStr.c_str();


                Here you add one pointer to other instead of concatenating the string. You need use std::string to concatenate, for example:



                const char* objDim = "The object dimension is: ";
                int objDimension = geo->Dimension();
                std::string objDimStr = objDim;
                objDimStr += std::to_string(objDimension);
                std::cout << objDimStr << std::endl;





                share|improve this answer


























                  0














                  objDim += objDimStr.c_str();


                  Here you add one pointer to other instead of concatenating the string. You need use std::string to concatenate, for example:



                  const char* objDim = "The object dimension is: ";
                  int objDimension = geo->Dimension();
                  std::string objDimStr = objDim;
                  objDimStr += std::to_string(objDimension);
                  std::cout << objDimStr << std::endl;





                  share|improve this answer
























                    0












                    0








                    0






                    objDim += objDimStr.c_str();


                    Here you add one pointer to other instead of concatenating the string. You need use std::string to concatenate, for example:



                    const char* objDim = "The object dimension is: ";
                    int objDimension = geo->Dimension();
                    std::string objDimStr = objDim;
                    objDimStr += std::to_string(objDimension);
                    std::cout << objDimStr << std::endl;





                    share|improve this answer












                    objDim += objDimStr.c_str();


                    Here you add one pointer to other instead of concatenating the string. You need use std::string to concatenate, for example:



                    const char* objDim = "The object dimension is: ";
                    int objDimension = geo->Dimension();
                    std::string objDimStr = objDim;
                    objDimStr += std::to_string(objDimension);
                    std::cout << objDimStr << std::endl;






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Nov 20 at 12:45









                    serge

                    59537




                    59537























                        -1














                        You're declaring objDim as a const, which means it can NOT be changed in runtime.



                        You should either use std::string instead of const char* or use strncat, as seen here: Append to the end of a Char array in C++






                        share|improve this answer


























                          -1














                          You're declaring objDim as a const, which means it can NOT be changed in runtime.



                          You should either use std::string instead of const char* or use strncat, as seen here: Append to the end of a Char array in C++






                          share|improve this answer
























                            -1












                            -1








                            -1






                            You're declaring objDim as a const, which means it can NOT be changed in runtime.



                            You should either use std::string instead of const char* or use strncat, as seen here: Append to the end of a Char array in C++






                            share|improve this answer












                            You're declaring objDim as a const, which means it can NOT be changed in runtime.



                            You should either use std::string instead of const char* or use strncat, as seen here: Append to the end of a Char array in C++







                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Nov 20 at 12:34









                            Rafael de Freitas

                            145




                            145






























                                draft saved

                                draft discarded




















































                                Thanks for contributing an answer to Stack Overflow!


                                • Please be sure to answer the question. Provide details and share your research!

                                But avoid



                                • Asking for help, clarification, or responding to other answers.

                                • Making statements based on opinion; back them up with references or personal experience.


                                To learn more, see our tips on writing great answers.





                                Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                                Please pay close attention to the following guidance:


                                • Please be sure to answer the question. Provide details and share your research!

                                But avoid



                                • Asking for help, clarification, or responding to other answers.

                                • Making statements based on opinion; back them up with references or personal experience.


                                To learn more, see our tips on writing great answers.




                                draft saved


                                draft discarded














                                StackExchange.ready(
                                function () {
                                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53393000%2fim-getting-a-compile-time-error-when-appending-a-string-to-a-const-char-in-c%23new-answer', 'question_page');
                                }
                                );

                                Post as a guest















                                Required, but never shown





















































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown

































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown







                                Popular posts from this blog

                                Costa Masnaga

                                Fotorealismo

                                Sidney Franklin