There are a lot of issues in your code that will result in problems:
- Your constructor
Macierz(int _x, int _y)
doesn’t work like you think it does. Onlywiersze[0]
will be valid after it finishes and any non-zero index will access unknown memory. The issue is really with your design of your 2D matrix class. I would get rid of the manually managed memory and just use a 2D vectorvector<vector<int>>
or even just a flat 1D vector and manage the index manually. - Your copy constructor
Macierz(Macierz &m)
doesn’t allocate memory for the new matrix and thus you are writing to unknown memory. - All your binary operators (+, -, *) allocate a new object and return it which will result in leaked memory. The canonical way is usually to modify the object and just
return *this;
for such operators. See this question for more details on operator overloading. - Your copy operator
Macierz::operator=(Macierz &m)
doesn’t check if the matrix is the same size or not. If it isn’t the same size you’d need to destroy and reallocate the matrix. - Your
Macierz::operator()(int x, int y)
doesn’t check the input indexes which may be on purpose but you may consider throwing an exception like with your other methods. - You have no destructor which will result in leaked memory, although if you use a
vector<>
you wouldn’t need one.
If you really need/want to implement a 2D matrix class from scratch you can search online for a large variety of existing code, like this one, as examples of what to do.
solved First chance exception without compiling errors