2

As the title suggests, I am wondering if I can call a non-member function (which is contained in the class header file) from inside the class, with the caveat that the nonmember function is using the class itself ? Normally I could just put the nonmember function above the class declaration, but that means I cannot pass it the class as input because it is not identified by then.

For reference this is what a slimmed down version of my "Matrix" class looks like, with the nonmember function "LUDecomposition", which I am trying to call from the "Determinant" member class:

#pragma once #include <vector> #include <tuple> enum MatrixType { Identity, Zeros, Ones }; class Matrix { private: int col, row; typedef std::vector<double> Row; std::vector<Row> data; public: Matrix(int columns, int rows): row(rows), col(columns), data(columns, std::vector<double>(rows)) { } Matrix(int columns, int rows, MatrixType matrixType) : row(rows), col(columns), data(columns, std::vector<double>(rows)) { switch (matrixType) { case Identity: this->MakeIdentity(); break; case Zeros: this->Fill(0); break; case Ones: this->Fill(1); break; default: break; } } Row& operator[](int i) { return data[i]; } std::tuple<int,int> Size() const { return std::make_tuple(col, row); } double Determinant() const { if (col != row) throw std::exception("Matrix must be square"); Matrix tempMatrix = *this; std::tuple<Matrix, Matrix> LU = LUDecomposition(tempMatrix); } }; std::tuple<Matrix, Matrix> LUDecomposition(Matrix& matrix) //This function decomposes input square matrix A into lower triangular matrix L and upper triangular matrix U such that A=LU (Doolittle) { std::tuple<int, int> size = matrix.Size(); int col = std::get<0>(size); int row = std::get<1>(size); Matrix lower(col, row); Matrix upper(col, row); for (int i = 0; i < col; i++) { for (int k = i; k < col; k++) { int sum = 0; for (int j = 0; j < i; j++) { sum += lower[j][i] * upper[k][j]; } upper[k][i] = matrix[k][i] - sum; } for (int k = i; k < col; k++) { if (i == k) lower[i][i] = 1; else { int sum = 0; for (int j = 0; j < i; j++) { sum += lower[j][k] * upper[i][j]; } lower[i][k] = (matrix[i][k] - sum) / upper[i][i]; } } } return std::make_tuple(lower, upper); } 

Is there any way at all to call this function from inside the class ? I don't mind if it remains a nonmember function, or is moved somewhere, just trying to find a way to do this.

5
  • 3
    Just supply a function prototype above the class; Commented Jun 9, 2022 at 21:06
  • 2
    You can also make it a static member of Matrix Commented Jun 9, 2022 at 21:07
  • 1
    Btw, why isn't it a member function? Commented Jun 9, 2022 at 21:30
  • 1
    @TedLyngmo I made it a nonmember since it needs the class itself passed into it. I tried passing the class as *this when it was a member function, but couldn't quite get it to work. I realise it might be a bit roundabout way but I have not much experience with C++ yet. If you think you can make it work as a member function then I'd welcome a suggestion. Commented Jun 9, 2022 at 22:10
  • 1
    But, the class itself, as in a Matrix&, is exactly what you get with a normal member function. Instead of matrix inside your function, you would just use the member variables and functions directly. Commented Jun 9, 2022 at 22:11

2 Answers 2

0

You need to do two forward declarations. One for the Matrix class and one for the LUDecomposition function.

class Matrix; // This lets LUDecomposition know a Matrix type exists std::tuple<Matrix, Matrix> LUDecomposition(Matrix& matrix); // this lets the Matrix class knows that a LUDecomposition function exists. 

Put them before the definitions of the class and function (e.g. below enum MatrixType) and it compiles.

Sign up to request clarification or add additional context in comments.

Comments

0

Looks like this code works fine

class A; void func(A); class A { }; void func(A) {} 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.