Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"used without template parameters"

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.

like image 976
synaptik Avatar asked Apr 21 '12 21:04

synaptik


People also ask

What is meant by the template parameters?

A template parameter is a special kind of parameter that can be used to pass a type as argument: just like regular function parameters can be used to pass values to a function, template parameters allow to pass also types to a function.

What are non-type parameters for templates?

A template non-type parameter is a template parameter where the type of the parameter is predefined and is substituted for a constexpr value passed in as an argument. A non-type parameter can be any of the following types: An integral type. An enumeration type.

Which is a correct example of template parameters?

For example, given a specialization Stack<int>, “int” is a template argument. Instantiation: This is when the compiler generates a regular class, method, or function by substituting each of the template's parameters with a concrete type.

Can template have default parameters?

You cannot give default arguments to the same template parameters in different declarations in the same scope. The compiler will not allow the following example: template<class T = char> class X; template<class T = char> class X { };


2 Answers

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

like image 116
Jon Purdy Avatar answered Sep 28 '22 05:09

Jon Purdy


You want this:

template <class T> int VisitedSet<T>::getSize() {     return vec.size(); } 
like image 39
chris Avatar answered Sep 28 '22 04:09

chris