0

I've the following classes:

class Person
{

    ...

    std::string personToString()
    {
        return "First Name: " + pFirstName + "\nLast Name: " + pLastName + 
        "\nID Type: " + determineIdType() + "\nID Number: " + std::to_string(pIdNumber) +
        "\nBirth Date: " + pBirthDate + "\nBiological Sex: " + determineBiologicalSex() +
        "\nPhone Number: " + pPhoneNumber;
    }

};



class Patient : Person
{
    ...     

    std::string patientToString() 
    {
        return "Patient's Data: " + "\n" + personToString() + 
        "\nHealth Insurance: " + determineHealthInsurance();
    }

};

When compiling, I get the following error:

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

What is my mistake here? This works in Java, I'm not sure why it doesn't on C++ (the concatenation of strings)

Thanks

var_null
  • 3
  • 3
  • 1
    *This works in Java* -- Never write C++ code using Java as a model. This is one such case where this rule holds. – PaulMcKenzie Mar 16 '20 at 20:05
  • Also, if you understood the error message, it is telling you that you are trying to apply `+` operator to two char arrays. You can't add char arrays, or arrays of any type for that matter using the `+` operator. – PaulMcKenzie Mar 16 '20 at 20:08
  • There is no operator. You have to convert one of the char arrays into something that can be added (a std::string, for example). – PaulMcKenzie Mar 16 '20 at 20:29

0 Answers0