Reputation: 5513
I'm thinking this is in my declaration but I'm not sure. Have a class "Matrix" which creates 2-dimensional arrays of type int. The class has several overloaded operators to perform arithmetic, etc. on the class objects.
One requirement is to check that the matrices have the same dimensions. Dimensions are stored as two private ints "dx" and "dy".
So to make this efficient I wrote a member function of type bool, as follows;
bool confirmArrays(const Matrix& matrix1, const Matrix& matrix2);
is the function header and the declaration is;
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;
}
}
but when I call confirmArrays
from within another member function I get this error;
Use of undeclared identifier confirmArrays
Calling function like so;
// 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
}
}
Upvotes: 0
Views: 2300
Reputation: 5513
The bool function is only needed to support the arithmetic functions. Some of those functions can be member overloaded operators, some can be friends. The trick here was to define the bool function as a friend also, making it accessible to member direct functions and friend functions alike, while retaining the ability to access the private member data.
friend bool confirmArrays(const Matrix& matrix1, const Matrix& matrix2);
Upvotes: 0
Reputation: 9691
Your operator*
isn't defined within the scope of Matrix
. You've actually defined a global operator. You need
Matrix Matrix::operator * (const Matrix& matrix1, const Matrix& matrix2)
{
...
}
and then it should be fine. NB, If this had compiled, you would have gotten a linker 'undefined reference to operator Matrix::operator*' error since that was not defined.
Upvotes: 1