Inheritence Issues With Templates

closed account (y3qpfSEw)
The following code when compiled with gcc declares that in the partial template specialization of Vector<T[2]> that the protected member data does not exist. What am I missing?

Matrix.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <cstddef>
template <typename T>
class Matrix {
    public:
    //! Define the type of the matrix.
    typedef T type;
    
    //! Overload the equality operator.
    Matrix& operator=(const type& rhs) {
        data = rhs;
        return *this;
    }
    
    //! Overload the reference operator to return the data.
    operator type&() {
        return data;
    }
    
    protected:
        //! The data that the matrix holds.
        type data;
};

template <typename T, size_t D>
class Matrix<T[D]> {
    public:
        //! Define the type of the specialized matirx.
        typedef Matrix<T> type[D];
        
        //! Define the index operator for the template specializtion.
        Matrix<T>& operator[](size_t index) {
            return data[index];
        }

    protected:
        //! The data that the matrix holds.
        type data;
};



Vector.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include "Matrix.hpp"

template <typename T>
class Vector : public Matrix<T> {};


template <typename T>
class Vector<T[2]> : public Matrix<T[3]> { // yes I do mean 3 for the matrix size.
  public:
    Vector(T x, T y) {
        data[0] = x;
        data[1] = y;
    }

    Vector(const Vector<T[2]>& copy) {}
    
    T& x;
    T& y;
};

I think if I got it right that you try to access a base data member through a derived object correct?

You should explicitly tell the compiler that this base member function exists. You have some options for that:

1) call the member function using this->

2) use a syntax like: using Matrix<T>::member_function;

3) declare it being a member of base object: Matrix<T>::member_function where you need it. This final has the disadvantage that turns off the virtualness of this function (if exists)

I hope that helps on your problem
closed account (y3qpfSEw)
Thanks for your help, is there any particular reason this specific example wouldn't compile? I notice when you don't use templates it does.
Does this have something to do with the fact that the compiler doesn't know the type of data at the time I inherit it?
I know it has to do with the Template metaprogramming. Since there is a chance that a template specialization exists complier is precautious not to search for member function unless it's explicitly told that there exists this member function.
how do you declate the using function, in a .cpp file so far visual studio doesn't like this:

CState_Menu class (its inherits CStates)
using CStates<CGAME_MANAGER>::Enter(CGAME_MANAGER*);

Here the error bit in the .cpp file
using CStates<CGAME_MANAGER>::Enter(CGAME_MANAGER* qp_laststate)

note:
Topic archived. No new replies allowed.