-1

On compiling it is showing sme error like use of delete function constexpr Player::Player(const Player&) when it is return the result of addition of the objects.

#include <bits/stdc++.h>

using namespace std;

class Player
{
  char* name;
  int num;

 public:
  Player(char* str = nullptr, int n = -1)
      : name{str}
      , num{n}
  {
    if (str != nullptr)
    {
      name = new char[strlen(str) + 1];
      strcpy(name, str);
      str = nullptr;
    }
  }

  Player& operator=(const Player& temp)
  {
    delete[] this->name;
    this->name = new char[strlen(temp.name) + 1];
    strcpy(this->name, temp.name);
    this->num = temp.num;
  }

  Player operator+(const Player& temp);
};

Player Player::operator+(const Player& temp)

{
  char* str = new char[strlen(name) + strlen(temp.name) + 1];

  strcpy(str, name);
  strcat(str, temp.name);

  int n = num + temp.num;

  Player result{str, n};

  delete[] str;

  return result;
}

int main()

{
  Player p1{"abc", 11};
  Player p2{" xyz", 9};
  Player p3;

  p3 = p1 + p2;
}
Roberto Caboni
  • 6,078
  • 10
  • 19
  • 34
  • 3
    [Why should I not #include ?](https://stackoverflow.com/q/31816095/5910058) / [Why is "using namespace std;" considered bad practice?](https://stackoverflow.com/q/1452721/5910058) – Jesper Juhl Jul 13 '20 at 15:11
  • 1
    [Can't reproduce](https://godbolt.org/z/eY36EW) I can see plenty of problems in your code, but not that one. – john Jul 13 '20 at 15:11
  • 2
    [Rule of 3/5](https://en.wikipedia.org/wiki/Rule_of_three_(C%2B%2B_programming)) – ChrisMM Jul 13 '20 at 15:12
  • You should take a look at [`std::string`](https://en.cppreference.com/w/cpp/string/basic_string) – Simon Kraemer Jul 13 '20 at 15:12
  • 3
    (1) Replace `char* name;` with `std::string name;` if you want an easy life. (2) You can then reintroduce the copy constructor with `=default` syntax; in fact you might not even need to define the constructors yourself. – Bathsheba Jul 13 '20 at 15:12
  • 1
    Fyi.. what does `Player& operator=(const Player& temp)` actually *return* ?? Look at the body of the function. See anything... missing ? – WhozCraig Jul 13 '20 at 15:13
  • You should spend some time learning about [smart pointers](https://en.cppreference.com/w/cpp/memory). – Jesper Juhl Jul 13 '20 at 15:14
  • the problem has been solved. – Vansh_Chaudhary Jul 13 '20 at 15:44
  • 1
    How did you solve it? You had so many errors. Did you address *all* of them, or are you just running a program that still has bugs, but the bugs no longer reveal themselves? – PaulMcKenzie Jul 13 '20 at 15:49

1 Answers1

0

According to the C++ 17 Standard (12.8 Copying and moving class objects)

7 If the class definition does not explicitly declare a copy constructor, one is declared implicitly. If the class definition declares a move constructor or move assignment operator, the implicitly declared copy constructor is defined as deleted; otherwise, it is defined as defaulted (8.4). The latter case is deprecated if the class has a user-declared copy assignment operator or a user-declared destructor.

Also the move constructor is defined as deleted at least because there is explicitly defined the copy assignment operator.

So you need to explicitly define the copy constructor that is required for the operator + to form the return object.

Pay attention to that the class definition has other drawbacks. For example the data member name can be equal to nullptr. This is allowed by the default constructor. In this case the cppy assignment operator can invoke undefined behavior due to this statement

this->name = new char[strlen(temp.name) + 1];
                      ^^^^^^^^^^^^^^^^^

And string literals have types of constant character arrays. So the first parameter of the default constructor shall be declared as having the type const char *.

Vlad from Moscow
  • 224,104
  • 15
  • 141
  • 268