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.
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.
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.
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.
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 { };
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(); }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With