I realize similar questions have been asked before, but I read a couple of those and still don\'t see where I\'m going wrong. When I simply write my class without separating th
You need to let your compiler know that you are implementing a method in template function:
template<typename T>
int VisitedSet<T>::getSize() {
return vec.size();
}
VisitedSet
is a template, not a class, so you can’t use VisitedSet
in a nested name specifier such as VisitedSet::getSize()
. Just as you specified the declaration of class VisitedSet<T>
for all class T
, you must specify the definition of VisitedSet<T>::getSize()
for all class T
:
template<class T>
int VisitedSet<T>::getSize() {
// ^^^
return vec.size();
}
The name of a template can, however, be used as though it were a class within a template definition:
template<class T>
struct Example {
Example* parent;
T x, y;
};
In this case, Example
is short for Example<T>
.
You want this:
template <class T>
int VisitedSet<T>::getSize() {
return vec.size();
}
You have to state the template parameter in the definition as well
template<class T>
int VisitedSet<T>::getSize() {
return vec.size();
}
otherwise the compiler cannot match it to the declaration. For example, there could be specializations for some parameter types.
Try putting
template <typename T>
above the implementation of VisitedSet::getSize() -- but beware that, in general, templated classes and functions should all be inlined. See the c++ faq here for more information.