"used without template parameters"

55,563

Solution 1

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

Solution 2

You want this:

template <class T>
int VisitedSet<T>::getSize() {
    return vec.size();
}

Solution 3

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();
 }

Solution 4

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.

Share:
55,563
synaptik
Author by

synaptik

Updated on July 17, 2022

Comments

  • synaptik
    synaptik almost 2 years

    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.