【发布时间】:2012-10-03 00:48:47
【问题描述】:
我认为这是在我的声明中,但我不确定。有一个类“Matrix”,它创建 int 类型的二维数组。该类有几个重载的运算符来对类对象执行算术等。
一个要求是检查矩阵是否具有相同的维度。存储尺寸 作为两个私有整数“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,因为它实际上并没有访问它所调用的对象)才能做到这一点。不知道它的调用方式和位置,猜测是最好的。
标签: c++ function undeclared-identifier