简体   繁体   English

从类内部调用非成员函数,但非成员函数将类作为输入(C++)

[英]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:作为参考,这是我的“Matrix”类的精简版本的样子,带有非成员函数“LUDecomposition”,我试图从“Determinant”成员类中调用它:

#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.我不介意它是否仍然是一个非成员函数,或者被移动到某个地方,只是想找到一种方法来做到这一点。

Looks like this code works fine看起来这段代码工作正常

class A;

void func(A);

class A
{

};

void func(A) {}

You need to do two forward declarations.你需要做两个前向声明。 One for the Matrix class and one for the LUDecomposition function.一个用于 Matrix 类,一个用于 LUDecomposition 函数。

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 (eg below enum MatrixType ) and it compiles.将它们放在类和函数的定义之前(例如,在enum MatrixType下方)并编译。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 How do I call a member function inside a non-member function in C++ with an object created outside the non-member function? - How do I call a member function inside a non-member function in C++ with an object created outside the non-member function? 从C调用C ++非成员函数 - Call C++ nonmember function from C C ++非成员函数,使用引用另一个类的类 - c++ non-member function using a class which references another class 模板类的C ++静态非成员函数返回对象 - C++ static non-member function returning object of a template class 来自非成员函数的C ++虚拟继承 - C++ Virtual Inheritance from a non-member function 为什么我不能在类之外重载C ++转换运算符,作为非成员函数? - Why can't I overload C++ conversion operators outside a class, as a non-member function? c ++ --direct—访问非成员函数体中的类成员 - c++ --direct— access of class members in non-member function bodies 使用非成员函数从输入流中提取类对象 - Using non-member function to extract class-objects from an input stream 如何在方法中调用接收 object 的非成员 function - How to call a non-member function that takes in an object within a method 模板成员函数不能从同一类C ++中调用非模板成员函数 - Templated member functions cannot call non-templated member function from same class, C++
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM