First, forward declare the Matrix class. This allows the Iterator class to see the name of the Matrix class, and make pointers and references to it. (It doesn't allow the Iterator class to access member data yet or call member functions yet.)
template<typename T, typename Size, typename Stack, typename Sparse>
class Matrix;
Then, define the Iterator class. All it can do at this point is hold references and pointers to the Matrix. (No access to Matrix's members yet.)
template<typename T, typename Size>
class Iterator{
// don't define any function bodies in here
//but do put all data members and prototypes in here
};
Then define the Matrix class (which can access Iterator members)
template<typename T, typename Size, typename Stack, typename Sparse>
class Matrix{
// don't define any function bodies in here
//but do put all data members and prototypes in here
};
Then define the method bodies for each class. At this point, the methods of both classes can access each other's members. Usually, this part goes in the .cpp file, but for templates it belongs in the .h file.
template<typename T, typename Size, typename Stack, typename Sparse>
Matrix<T,Size,Stack,Sparse>::Matrix(){ /*...*/}
template<typename T, typename Size>
Iterator<T,Size>::Iterator(){ /*...*/ }