can you copypaste your code somewhere (pastebin, hastebin), it is very small on this screenshot to read it from phone
probably calling delete[] on an already deleted array.
by the way, why don't you use std::string?
https://pastebin.com/FX7n5BJN https://pastebin.com/FSrBd3Z2 https://pastebin.com/D3VSaPKB
here are 3 file of my code. 2 for class and 1 for main
Your string created on stack as array of char (name1 in main()) then you call delete on it in the destructor of Player. And that because of this code in the constructor char* buffer = name; int name_size = strlen(buffer); name = new char [name_size + 1]; this->name = buffer; this->symbol = symbol; You assign name (input parameter) to buffer and then assign buffer to this->name.
i cannot use std::string on this project (course rules...)
Ah, that's bad. Because in C++ we don't write code like this usually, it's very error prone (and extra verbose).
Which way is good to fix it without using std::string?
what about smart pointers like std::shared_ptr<std::array<char>> ?
If feels like the code in the constructor is not entirely correct, you probably wanted to assign result of new directly to this->name? Also, pass name as const char* not char*, then compiler will not let you modify it accidentally
also, it seems like you need to do strcpy there, not assign string one to another
Обсуждают сегодня