So, I'm developing a templated Vector class for an assignment and I came across a few issues that I solved by Googling, but I still want to know why what I was doing was wrong. First issue is that I had:
template <typename T>
class Vector
{
...
template <typename T2>
Vector(const Vector<T2> &other);
}
template <typename T, T2>
Vector<T>::Vector(const Vector<T2> &other)
{
...
}
this was giving me an "unable to match function definition to an existing declaration" in VS11. I fixed it by putting the template definitions on separate lines:
template <typename T>
template <typename T2>
Vector<T>::Vector(const Vector<T2> &other)
{
...
}
but I still don't know why this was needed. I know that the first definition is valid for functions/classes that use multiple templates in them, but why is it the case that the syntax changes when you mix a templated class with a templated member function?
My second question has to do with types inside of template classes. When writing the iterator I had functions such as:
template <typename T>
class Vector
{
...
class iterator
{
...
iterator &operator++(void);
}
}
template <typename T>
Vector<T>::iterator &Vector<T>::iterator::operator++(void)
{
...
}
which gave me "dependent name is not a type" and I later found out that I needed to add "typename" in front:
template <typename T>
typename Vector<T>::iterator &Vector<T>::iterator::operator++(void)
{
...
}
After Googling the warning number (which resulted in an error), I realized why the error exists, but it isn't too obvious why the compiler doesn't know that Vector<T>::iterator
is a type. I mean, it has the definition of the class, so...
Anyway, thanks for clarifying these few things for me!