Matrix and vector multiplication in different classes

I want to calculate the matrix multiplication with a matrix and a vector.

the matrix and vector have their own class files, and I want to multiply a vector with a matrix.

To do so I cannot multiply a vector with a matrix in a single class file, because they have different dimensional arrays (obviously).

My main function here is where the matrix is multiplied.

1
2
3
4
5
6
7
8
9
int main() {

		Matrix mat1(3, 3, 1.0);
		mat1(1, 2) = 11.12;
		mat1(0, 0) = 4.4;
		Vector vec1{6.4,4.2,4.2};
		Vector vec2 = mat1 * vec1; /* here is an error because matrix don't exist in vector class*/
		std::cout << "vec2:" << vec2 << std::endl;;
}


this is my constructor for the matrix and vector multiplication by overloading the * operator:

This is the constructor for the vector class
1
2
3
4
5
6
7
8
9
10
11
12
13
14
Matrix operator*(Matrix matrix, Vector vector)
{
	double sum_of_elements = 0;
	Vector res(vector.Index);

	for (int i = 0; i <matrix.getnumcols; i++)
	{
			for (int m = 0; m < vector.Index; m++) {
				sum_of_elements++;
				std::cout<< sum_of_elements <<std::endl;
		}
	}
	return res;
}


in the header file, I even used friend keyword to include the Matrix header file but I can't make it work.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <vector>
#include <iostream>
#include "Matrix.h"
class Vector
{
	friend std::ostream &operator<<(std::ostream &out, const Vector &vec);
	private:
		int Index;
		std::vector<float> cell;
	public:
		Vector();
		Vector(float a, float b, float c);
		friend Matrix operator*(Matrix matrix, Vector vector);/* the friend of Vector, the Matrix*/
	~Vector();


what should I do to solve this?

Note that the main function shall NOT be modified here only the constructor and the header
Last edited on
What is the error message?

Edit:

In your operator*, you are trying to return a vector, when it expects a Matrix. Make the return type be Vector.
[Matrix]*[Vector] = Vector.

Oh, it also sounds like you're having a dependency issue... I think I see issue (vector needs to know what a matrix is, matrix needs to know what a vector is), but it's hard to formulate what the exact correct code is in my head.

If the only reason that "Matrix.h" needs to be #included in Vector's header file is for the operator, then declare it like this instead:

1
2
3
4
5
6
// no #include "Matrix.h"
class Matrix;

class Vector {
    friend Vector operator*(const Matrix& matrix, const Vector& vector);
};


And then in the implementation file, you can #include "Matrix.h"

Basically, you use a reference there because you need to avoid the situation where the Vector needs to know the full definition of a Matrix before it can be created if the Matrix also needs to know the full definition of a Vector.
Last edited on
This is my Error message:

E0312 no suitable user-defined conversion from "Matrix" to "Vector" exists Project2_LA Source.cpp 17

Error C3867 'Matrix::getnumcols': non-standard syntax; use '&' to create a pointer to member vector.cpp 23

Error C2664 'Vector::Vector(const Vector &)': cannot convert argument 1 from 'int' to 'const Vector &' vector.cpp 21

Error C2446 '<': no conversion from 'int (__cdecl Matrix::* )(void)' to 'int' vector.cpp 23

Error C2440 'return': cannot convert from 'Vector' to 'Matrix' vector.cpp 30

Error C2440 'initializing': cannot convert from 'Matrix' to 'Vector' source.cpp 17
Do we see the lines of code that spawn errors?
I don't. That makes it impossible to explain (to you) why that code has that error.

Your interface for Matrix clearly has method to modify element of matrix. If the public interfaces of both Matrix and Vector had methods to read and modify elements, then neither operator* nor operator<< has to be a friend. Prefer non-friend non-member functions.


All uses of an identifier do not require the full definition of the identifier:
https://herbsutter.com/2013/12/31/gotw-7b-solution-minimizing-compile-time-dependencies-part-2/
The real problem is the manipulation of matrix and vector in vector class
Topic archived. No new replies allowed.