C++ – “used without template parameters”

c++classtemplates

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 the prototype from the definition, everything works fine. The problem happens when I separate the prototype and definition as shown below:

template<class T> class VisitedSet { 
public:
    VisitedSet(); 
    int getSize(); 
    void addSolution(const T& soln); 
    void evaluate(); 
private:
    vector<T> vec;
    int iteration;
};

And as an example of a definition that gives me this error:

int VisitedSet::getSize() {
    return vec.size();

I've never made a templated class before, so please pardon me if the problem here is trivial.

Best Solution

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>.