“used without template parameters”

后端 未结 5 2037
慢半拍i
慢半拍i 2021-02-01 03:34

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

相关标签:
5条回答
  • 2021-02-01 03:42

    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();
     }
    
    0 讨论(0)
  • 2021-02-01 03:52

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

    0 讨论(0)
  • 2021-02-01 03:53

    You want this:

    template <class T>
    int VisitedSet<T>::getSize() {
        return vec.size();
    }
    
    0 讨论(0)
  • 2021-02-01 03:54

    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.

    0 讨论(0)
  • 2021-02-01 04:00

    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.

    0 讨论(0)
提交回复
热议问题