내 선언문에있는 것 같지만 잘 모르겠습니다. int 형의 2 차원 배열을 생성하는 "Matrix"클래스를 가져야합니다. 클래스에는 클래스 객체에 대해 산술 등을 수행 할 수있는 오버로드 된 연산자가 여러 개 있습니다.C++ 멤버 함수 오류 "선언되지 않은 식별자"가 다른 멤버 함수 내에서 사용되는 경우
하나의 요구 사항은 행렬의 치수가 동일한 지 확인하는 것입니다. 치수는 두 개의 전용 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')로 만들어야 할 것입니다. 그것이 어디서 어떻게 불려지는지 보지 않고도 당신이 얻을 수있는 최고의 추측이됩니다. –