0

I am trying out operator overloading and I can't figure out why my program keeps crashing.

When I dereference p.name in the operator<< function it crashes, and when I don't refenece it, it just prints out an address of the string and the program does not crash. The core of the problem might be in setting object p1 equal to the return of the operator+ function, but I cant figure out what exactly happens there.

main.cpp

int main()
{
    Player p1("John", 10);
    Player p2("Jon");
    cout<<p1<<endl;
    cout<<p2<<endl;
    p1=p2+20;
    cout<<p1;
}

player.h

class Player
{
    public:
        Player(string _name);
        Player(string _name, int _score);
        ~Player();
        friend ostream& operator<<(ostream& os, Player& p);
        Player operator+(int p);

    private:
        string *_name;
        int _score;
};

player.cpp


Player::Player(string name)
{
    _name=new string;
    *_name = name;
    _score = 0;
}
Player::Player(string name, int score)
{
    _name=new string;
    *_name = name;
    _score = score;
    cout<<"Konst1"<<endl;
}
Player::~Player()
{
    delete _name;
    _name = 0;
}
ostream& operator<<(ostream& os, Player& p)
{
    os<<*p._name<<" won: "<<p._score<<" pts";
    return os;
}
Player Player::operator+(int p)
{
    Player obj(*_name, _score+p);
    return obj;
}

Expected result would be "John won: 20 pts".

Edit: Putting string _name on heap memory is the part of the task I was given, so I have to try to make it work like that.

  • 1
    Why are you using a `string *_name;` pointer at all? Any specific reason to do so instead of simply using `string _name;`? – πάντα ῥεῖ May 03 '19 at 15:22
  • 1
    You can fix the problem by following [The Rule of Three](http://stackoverflow.com/questions/4172722/what-is-the-rule-of-three) but you can avoid the problems by storing a `string` object instead of a pointer to a `string` object. – R Sahu May 03 '19 at 15:23
  • Agree with @πάνταῥεῖ. `string *_name;` is unusual. Use `string _name;` and check if it is valid with `if (!_name.empty()) {...}`. – jww May 03 '19 at 15:24
  • Implementing `operator+` simply exposed a different preexisting problem with your class. You did not follow the [rule of 3/5/0](https://en.cppreference.com/w/cpp/language/rule_of_three). Either implement the missing members or simplify your class such that the compiler generated ones are correct (eliminate the usage of a raw pointer). – François Andrieux May 03 '19 at 15:24
  • Putting string _name on heap memory is the part of the task I was given. – Markus Harkonnen May 03 '19 at 15:25
  • 1
    `std::string` already manages the backing store of the data in the string. Why are you raw-pointer managing the string that manages the data? – Eljay May 03 '19 at 15:25
  • If you are required to use `string *_name;` because of constraints beyond your control, please followi the [The Rule of Three](http://stackoverflow.com/questions/4172722/what-is-the-rule-of-three) – R Sahu May 03 '19 at 15:27
  • Thank you! The Rule of Three solved the problem. – Markus Harkonnen May 04 '19 at 11:36

0 Answers0