我在想這是在我的聲明中,但我不確定。有一個創建類型爲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',因爲它實際上並不訪問它所調用的對象)才能做到這一點。沒有看到它的名稱和方式,猜測就是最好的。 –