i have little problem with gauss elemination and vector:

Hello, I have a little problem with the Gauss elimination:

this is my main.cpp file where i was tasked to make a static class fucntion in order to call to constructor (linalg.cpp)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <vector>
#include "Matrix.h"
#include "Vector.h"
#include "linalg.h"
#include "Source.h"

int main() {
	try {

		Matrix m1{ {1, 2, 3}, //
				  {4, 5, 6} };
		Vector v1 = linalg::gaussElimination(m1);//this is the code i want to run
		std::cout << "v1:" << v1;

	}
	catch (std::exception &e) {
		std::cout << "Exception: " << e.what() << "!" << std::endl;
	}
	catch (...) {
		std::cout << "Unknown exception caught!" << std::endl;
	}
return 0;
}



this is my constructor lingalg.cpp, but it as a problem down there the Vector x(n), my code seeming to give an error:

"Vector does not define this operator or a conversion to a type acceptable to the precefined operator."


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
Vector linalg::gaussElimination(Matrix m)
{
		int n = m.Cols;
		for (int i = 0; i < n; i++) {
			double maxEl = abs(m.cell[i][i]);
			int maxRow = i;
			for (int k = i + 1; k < n; k++) {
				if (abs(m.cell[k][i]) > maxEl) {
					maxEl = abs(m.cell[k][i]);
					maxRow = k;
				}
			}
			for (int k = i; k < n + 1; k++) {
				double tmp = m.cell[maxRow][k];
				m.cell[maxRow][k] = m.cell[i][k];
				m.cell[i][k] = tmp;
			}
			for (int k = i + 1; k < n; k++) {
				double c = -m.cell[k][i] / m.cell[i][i];
				for (int j = i; j < n + 1; j++) {
					if (i == j) {
						m.cell[k][j] = 0;
					}
					else {
						m.cell[k][j] += c * m.cell[i][j];
					}
				}
			}
		}
		Vector x(n);// this is my new operator to return value
		for (int i = n - 1; i >= 0; i--) {
			x[i] = m.cell[i][n] / m.cell[i][i];// i have problem with x[i]
			for (int k = i - 1; k >= 0; k--) {
				m.cell[k][n] -= m.cell[k][i] * x[i];
			}
		}
	return x;
}


to make the code programmable i added Vector.cpp and Vector.h files to make the code work.

1
2
3
4
5
6
Vector::Vector(int n)
{
	for (int i = 0; i < n; i++) {
		element.push_back(0);
	}
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

#pragma once
#include <vector>
#include <iostream>
#include "Matrix.h"
#include <initializer_list>
class Vector : public Matrix
{
	private:
		int Index;
		std::vector<float> element;

	public:
		Vector(int n);



Is there any solution or alternate ones?
Last edited on
Assuming that the complaint is that the operator
Vector::operator[]
does not exist, create that operator.
ok
Topic archived. No new replies allowed.