|
-
April 28th, 2006, 05:29 AM
#1
Strange protection error
I'm trying to implement a generic N-Dimensional matrix class but I can't to get a pointer to a protected data member. Here's the code cut down considerable.
Code:
template <typename NumType, int dim1, int dim2>
class Matrix
{
protected:
NumType m[dim1][dim2];
};
template <typename NumType, int dim1>
class SquareMatrix : public Matrix<NumType, dim1, dim1>
{
public:
//Return the minor matrix
SquareMatrix<NumType, dim1 - 1> Minor(int i, int j) {
SquareMatrix<NumType, dim1 - 1> ret;
int *pretm = (int*)&ret.m[0][0]; //compiler hates this
for (int u = 0; u < dim1; u++)
{
for (int v = 0; v < dim1; v++)
{
if( (u!=i) && (v!=j) ) //as long as we are not on a row or column of i or j then put the number into next slot of matrix
{
*pretm = m[u][v];
pretm++;
}
}
}
return ret;
}
};
template<typename NumType>
class SquareMatrix<NumType, 2> : public Matrix<NumType, 2, 2>
{
public:
//specialized code for 2x2 matrices
};
int main(int argc, char *argv[])
{
int arr3[3][3] = { {1, 2, 3},
{0, 5, 6},
{0, 8, 9} };
SquareMatrix<int, 3> m(arr3);
cout << m.Determinant() << endl; //determinant will call Minor()
return EXIT_SUCCESS;
}
When I try to compile this I get the following errors.
/home/bobby/meta/src/meta.cpp:106: instantiated from `NumType SquareMatrix<NumType, dim1>::Cofactor(int, int) [with NumType = int, int dim1 = 3]'
/home/bobby/meta/src/meta.cpp:116: instantiated from `NumType SquareMatrix<NumType, dim1>: eterminant() [with NumType = int, int dim1 = 3]'
/home/bobby/meta/src/meta.cpp:231: instantiated from here
/home/bobby/meta/src/meta.cpp:35: error: `int Matrix<int, 2, 2>::m[2][2]' is protected
/home/bobby/meta/src/meta.cpp:90: error: within this context
Any ideas about why the compiler considers m to be protected in my Minor function? Everything is fine if I make m public. Thanks.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|