c++classparameter-passingnon-member-functions

Call non-member function from inside class, but the nonmember function takes class as input (C++)


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.


Solution

  • 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.