-4

I want to overload operator + in array that consider char .I want to print Hello Jack But it has eror in my program . How can I fix it?

 class Test
{
private:
    char s1, s2;
public:
    Test() {}
    Test(char a, char b) { s1 = a;  s2 = b; }
    void Print() { cout << "String1 :" << s1 << "\tString2 :" << s2 << endl; }

    Test operator+(const Test& r)
    {
        Test temp;
        temp.s1 = s1 + r.s1;
        temp.s2 = s2 + r.s2;
        return temp;
    }


};
//////////////////////////////////////////////////////////////////////////
int main()
{
    char str1[] = "Hello";
    char str2[] = " Jack ";

    
    Test t1(str1[]);
    Test t2(str2[]);


    Test temp;
    temp = t1 + t2;
    temp.Print();

}
Scheff's Cat
  • 19,528
  • 6
  • 28
  • 56
pary
  • 1
  • 2
  • 2
    Changing this to char does not help. Also note that `char` is a single character not a c-string and not a `std::string` – drescherjm May 28 '21 at 17:10
  • 1
    `class Test` can only hold 2 characters `char s1, s2;` it's never going to be able to hold a c-string or be able to add 2 c-strings together. I recommend the [booklist](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list/388282#388282) – Richard Critten May 28 '21 at 17:12
  • You cannot store multiple `char`s into one `char`. You have to learn about memory management. Or, use `std::string`. It's well-prepared for your use case. – Scheff's Cat May 28 '21 at 17:12
  • For your new code you can `Test t1(str1[0]);` and `Test t2(str2[0]);` however I am not sure what operator+ is supposed to do. It can't add additional characters. – drescherjm May 28 '21 at 17:13
  • If you are forced to use c-strings with this code and can not use `std::string` you most likely need to allocate a buffer, implement the rule of 3 or 5 and use `char*` instead of char. – drescherjm May 28 '21 at 17:15
  • You failed to initialize `s1` and `s2` in the default `Test` constructor. – PaulMcKenzie May 28 '21 at 17:15
  • it didnt work @drescherjm – pary May 28 '21 at 17:15
  • Change `char s1, s2;` to `std::string s1, s2; ` and add `#include ` at the top of the file. – drescherjm May 28 '21 at 17:16
  • 4
    `Test t1(str1[]);` -- Tell us what you're trying to do with that line of code. I really think you're not reading proper C++ learning material, as you will never see a line of code that looks like this in any good C++ book. – PaulMcKenzie May 28 '21 at 17:17

1 Answers1

0

You're defining pretty much everything wrong.
Your +operator overload sums up the two chars that are part of the "Test", but you only ever pass one of the two to separate Test objects.

If you want to use strings, use std::string instead of arrays of chars, unless you have a particular reason to do so, but at this point this is the least of your problems.

friend Test operator+(Test lhs, const Test& rhs) is how you'd define the return of a new object based on the values of two of the same objects.

You'd then have no need for a print function as it would just be put as-is in an iostream once you override that functionality as well, which would really amount to just returning the stored string (string singular, you only need one per object here).

In its current state, I highly doubt your code even compiles as you are passing one argument to an object that only takes none or two.

thanat0sis
  • 185
  • 1
  • 12