Zwracanie klasy z vectorem

0

Piszę klasę macierzy ( dla lepszego zrozumienia o co w tym chodzi :) ). Muszę zwrócić metodą klasy Matrix klasę Matrix, która to zawiera dwuwymiarowy vector. Gdy próbuje odczytać przed zwróceniem (jeszcze w metodzie klasy) wszystko jest okej. Tuż po zwróceniu i wyświetleniu wyniku otrzymuje same zera ;/ Metoda ta ma usuwać daną kolumnę i dany wiersz z obecnej macierzy. Poniżej zamieszczam odpowiedni fragment kodu:

Metoda:

 

#pragma once
#include <iostream>
#include <vector>

class CMatrix
{
public:
	CMatrix(unsigned int row, unsigned int col);
	CMatrix(const CMatrix & m);
	~CMatrix();

	friend std::ostream & operator<<(std::ostream & out, const CMatrix & A);
	friend std::istream & operator>>(std::istream & in, CMatrix & A);

	inline unsigned int getRows() const { return rows; }
	inline unsigned int getColumns() const { return columns; }
	double getValue(unsigned row, unsigned col) const;
	void setValue(unsigned row, unsigned col, double val);
	double getDet();

	double det(CMatrix m) const;
	CMatrix removeRowsAndCol(unsigned int row, unsigned int col) const;

private:
	const unsigned int rows;
	const unsigned int columns;
	std::vector<std::vector<double>> matrix;
};




#include "Matrix.h"

CMatrix::CMatrix(unsigned int row, unsigned int col) : rows(row), columns(col)
{
	matrix.resize(row);
	
	for (unsigned i = 0; i < row; i++)
	{
		matrix[i].resize(columns);
	}
}

CMatrix::CMatrix(const CMatrix & m) 
	: rows{ m.getRows()}, columns { m.getColumns()}
{
	matrix.resize(rows);
	for (unsigned i = 0; i < rows; i++)
	{
		matrix[i].resize(columns);
	}
}

CMatrix::~CMatrix()
{
}

double CMatrix::getValue(unsigned row, unsigned col) const
{
	if (row >= getRows() || col >= getColumns())
	{
		std::cerr << "\n\n\tPrzekroczono tablice vector!\n\n";
		throw;
	}

	return matrix[row][col];
}


void CMatrix::setValue(unsigned row, unsigned col, double val)
{ 
	if (!(row < rows && col < columns))
	{
		std::cout << "Error!\n\tPrzekroczono zasieg!\n";
		throw;
		return;
	}

	matrix[row][col] = val;

	return;
}


double CMatrix::getDet()
{
	if (rows != columns)
	{
		std::cerr << "\n\nBlad! Macierz nie jest kwadraowa... \tNie mozna obliczyc wyznacznika!\n\n";
		return 0.0;
	}

	return det(*this);
}


double CMatrix::det(CMatrix m) const
{
	double ret = 0.0;

	if (m.columns == 2)
		return ((matrix[0][0] * matrix[1][1]) - (matrix[0][1] * matrix[1][0]));

	for (unsigned i = 0; i < rows; i++)
	{
		ret += matrix[0][i] * pow(-1, 1 + i) * det(removeRowsAndCol(0, i));
	}

	return ret;
}


CMatrix CMatrix::removeRowsAndCol(unsigned int row, unsigned int col) const
{
	if (row >= getRows() || col >= getColumns()) throw;

	CMatrix ret(getRows() - 1, getColumns() - 1);
	
	unsigned plus_k = 0;
	unsigned plus_l = 0;

	for (unsigned k = 0; k < ret.getRows(); k++)
	{
		if (k == row) plus_k = 1; 

		for (unsigned l = 0; l < ret.getColumns(); l++)
		{
			if (l == col) plus_l = 1; 
			double val = getValue(k + plus_k, l + plus_l);
			ret.setValue(k, l, val);
		}
	}

	std::cout << ret;

	return ret;
}

 
int main()
{
	CMatrix A(3, 3);
	for (int i = 0; i < 3; i++)
	{
		for (int j = 0; j < 3; j++)
		{
			A.setValue(i, j, i + j + 10);
		}
	}

	std::cout << A.removeRowsAndCol(1, 1); ///Tutaj wyświetla same zera ;(

	return 0;
}

<image>2e53eea0a6.png</image>

Z góry dziękuje za odpowiedź :)

0

Bez kodu tej klasy to można co najwyżej wróżyć.

1

Tak jak podejrzewałem, masz konstruktor kopiujący, który nie kopiuje a jedynie zmienia rozmiar wektorów, stąd te zera.

2
CMatrix CMatrix::removeRowsAndCol(unsigned int row, unsigned int col)

Zwracasz kopię klasy, więc konstrukt kopiujący jest wywołany. I przestań odpowiadać na temat w komentarzu.

0

Okej. Dzięki wielkie :)

1

Przede wszystkim: po to masz wektory i inne cuda, żeby m.in. nie musieć pisać własnego konstruktora kopiującego. Jak usuniesz ten konstruktor, to ten kod nagle zacznie "magicznie" działać prawidłowo (zakładając że nie ma innego buga).
Tak samo pisanie pustego destruktora jest bez sensu.

2

Przyjrzyjmy się jak wygląda klasa po wywaleniu metod - same pola:

class CMatrix
{
    const unsigned int rows;
    const unsigned int columns;
    std::vector<std::vector<double>> matrix;
};

dwa inty i vector. żadnych wskaźników. znaczy - niepotrzebny jest ani destruktor ani operator przypisania, ani konstruktor kopiujący. inty nie potrzebują w ogóle, a vector ma konstruktor i destruktor które robią to co trzeba.

1 użytkowników online, w tym zalogowanych: 0, gości: 1