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?
#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;
};
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)
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.