invalid operands const char* to binary 'operator+'

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • SwissProgrammer
    New Member
    • Jun 2020
    • 220

    invalid operands const char* to binary 'operator+'

    A question from help I was getting on page https://bytes.com/topic/c/answers/974514-what-minimum-requirements-all-17-planes-unicode-c#post3829190



    There is a lot on that page, so I separated this into a new question.


    Previous:
    Code:
        string tempString = "";
        string stringByte;
        string words;
    
        for (char& _char : words) {
            numToString = to_string (i);;
            stringByte = bitset<8>(_char).to_string();
    // etc.
    I get the following error if I do not split up the next two lines

    error: invalid operands of types 'const char*' and 'const char [23]' to binary 'operator+'

    With error:
    Code:
    tempString = "\n      [" + _char + "]=<Combined_1 PLANE 0>" + stringByte;

    Without error:
    Code:
                            tempString = "\n      [" + _char;
                            tempString = tempString  + "]=<Combined_1 PLANE 0>" + stringByte;

    I have been trying to understand this.
    What is it saying that I need to do?


    Help please.

    Thank you.
  • dev7060
    Recognized Expert Contributor
    • Mar 2017
    • 656

    #2
    error: invalid operands of types 'const char*' and 'const char [23]' to binary 'operator+'
    The overloaded operator+ expects at least one operand to be of string type. Try this:
    Code:
    tempString = string("\n      [") + _char + string("]=<Combined_1 PLANE 0>") + stringByte;

    Comment

    • SwissProgrammer
      New Member
      • Jun 2020
      • 220

      #3
      dev7060,

      Thank you.

      You said, "at least one operand" therefore I have been studying how to do this with the other parts:
      _char
      I found Microsoft documentation at https://www.techiedelight.com/conver...to-string-cpp/
      and
      stringByte
      is already a string, but not quite acceptable because it filled it with
      Code:
      stringByte = bitset<8>(_char).to_string();
      which was not accepted by the compiler as a string.
      I guess that I have to re-make it a string.

      (I expect that "re-make it" is not the correct wording, but I am not certain how to say that.)

      Using your guidance, I have studied this (some) and now can use this:
      Code:
                      string a1 = "\n      [";
                      string a2(1, _char);
                      string a3 = "]=<Combined_1 PLANE 0>";
                      string a4 = stringByte;
      
      //                tempString = string("\n      [") + _char + string("]=<Combined_1 PLANE 0>") + stringByte;
                      tempString = a1 + a2 + a3 + a4;/


      Thank you.

      Comment

      Working...