[英]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.