我在想这是在我的声明中,但我不确定。有一个创建类型为int的2维数组的类“Matrix”。该类有几个重载操作符来对类对象执行算术等。当在另一个成员函数内使用C++成员函数错误“未声明标识符”
一个要求是检查矩阵具有相同的尺寸。尺寸被存储为 作为两个私人整数“dx”和“dy”。
所以为了使这个效率更高,我写了一个类型为bool的成员函数,如下所示;
bool confirmArrays(const Matrix& matrix1, const Matrix& matrix2);
是函数头和声明;
bool Matrix::confirmArrays(const Matrix& matrix1, const Matrix& matrix2)
{
if (matrix1.dx == matrix2.dx && matrix1.dy == matrix2.dy)
{
// continue with operation
return true;
} else {
// hault operation, alert user
cout << "these matrices are of different dimensions!" << endl;
return false;
}
}
但是当我打电话confirmArrays
从另一个成员函数中我得到这个错误;
使用未声明的标识符的confirmArrays
调用像这样的功能;
// matrix multiplication, overloaded * operator
Matrix operator * (const Matrix& matrix1, const Matrix& matrix2)
{
Matrix product(matrix1.dx, matrix2.dy);
if (confirmArrays(matrix1, matrix2))
{
for (int i=0; i<product.dx; ++i) {
for (int j=0; j<product.dy; ++j) {
for (int k=0; k<matrix1.dy; ++k) {
product.p[i][j] += matrix1.p[i][k] * matrix2.p[k][j];
}
}
}
return product;
} else {
// perform this when matrices are not of same dimensions
}
}
需要看到您的调用代码,我认为。 –
其实我认为如果调用函数在'confirmArrays'之前声明的话 - 很难看到它可能是什么。编辑 - 只是做了一点测试,顺序应该不重要,但也许尝试。 –
疯狂的猜测:你是从'const'成员函数调用它的吗?你需要使它成为'const'(或者更好的是'static',或者可能是'friend',因为它实际上并不访问它所调用的对象)才能做到这一点。没有看到它的名称和方式,猜测就是最好的。 –