CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Dec 2004
    Location
    Paso de Robles
    Posts
    296

    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.

  2. #2
    Join Date
    Aug 2002
    Location
    Cluj-Napoca,Romania
    Posts
    3,496

    Re: Strange protection error

    SquareMatrix<int, 3> and SquareMatrix<int, 2> are (almost) unrelated types. They do share the same base class but there is no other connection between them: they are not friends, they do not inherit each etc. So there is no reason for SquareMatrix<int, 3> to access the protected/private members from SquareMatrix<int, 2>.
    Har Har

  3. #3
    Join Date
    Dec 2004
    Location
    Paso de Robles
    Posts
    296

    Re: Strange protection error

    Thanks, it makes sense now. Do you think I should provide an accessor function to a pointer of the first element of the array or somehow make them friends?

  4. #4
    Join Date
    Feb 2005
    Location
    Normandy in France
    Posts
    4,590

    Re: Strange protection error

    SquareMatrix<int,3,3> has access to Matrix<int,3,3> protected members (since it is derived from it), but not to protected members of Matrix<int,2,2>

    Your program has flaws.
    SquareMatrix should not derive from Matrix!
    In fact Matrix is a useless class, since it has no public interface, and thus, can't be a useful base class.
    Moreover your code uses public inheritance, while private inheritance would be better : If someone derive from your matrix, it should not be able to access implementation details.

    You should rather put m as a public member of Matrix, and Matrix as a private member of SquareMatrix.


    int *pretm = (int*)&ret.m[0][0]; //compiler hates this
    Incorrect! &ret.m[0][0] is a int (*)[dim2]. It can't be safely converted to int*, because there may be some padding at the end of an array.

    *pretm = m[u][v];
    This should be *pretm = this->m[u][v];

    Other compile-time errors are trivial...
    "inherit to be reused by code that uses the base class, not to reuse base class code", Sutter and Alexandrescu, C++ Coding Standards.
    Club of lovers of the C++ typecasts cute syntax: Only recorded member.

    Out of memory happens! Handle it properly!
    Say no to g_new()!

  5. #5
    Join Date
    Dec 2004
    Location
    Paso de Robles
    Posts
    296

    Re: Strange protection error

    Wow, I'm glad I posted. There are actually a lot more functions in the Matrix class than I posted. I wanted to make looking through the code easier. I was actually going to implement operator overloads for addition and multiplication in the base class, since this is a generic property between matrices. That's why I was using public inheritance. Otherwise, I would have to implement an arithmetic operators in every derived class.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured