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.
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();
}
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.
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();
}
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.
Related
If we have a standard class:
class Foo {
public:
int fooVar = 10;
int getFooVar();
}
The implementation for getFooVar() would be:
int Foo::getFooVar() {
return fooVar;
}
But in a templated class:
template <class T>
class Bar {
public:
int barVar = 10;
int getBarVar();
}
The implementation for getBarVar() must be:
template <class T>
int Bar<T>::getBarVar(){
return barVar();
}
Why must we have the template <class T> line before the function implementation of getBarVar and Bar<T>:: (as opposed to just Bar::), considering the fact that the function doesn't use any templated variables?
You need it because Bar is not a class, it's a template. Bar<T> is the class.
Bar itself is a template, as the other answers said.
But let's now assume that you don't need it, after all, you specified this, and I added another template argument:
template<typename T1, typename T2>
class Bar
{
void something();
};
Why:
template<typename T1, typename T2>
void Bar<T1, T2>::something(){}
And not:
void Bar::something(){}
What would happen if you wanted to specialize your implementation for one type T1, but not the other one? You would need to add that information. And that's where this template declaration comes into play and why you also need it for the general implementation (IMHO).
template<typename T>
void Bar<T, int>::something(){}
When you instantiate the class, the compiler checks if implementations are there. But at the time you write the code, the final type (i.e. the instantiated type) is not known.
Hence the compiler instantiates the definitions for you, and if the compiler should instantiate something it needs to be templated.
Any answer to this question boils down to "because the standard says so". However, instead of reciting standardese, let's examine what else is forbidden (because the errors help us understand what the language expects). The "single template" case is exhausted pretty quickly, so let's consider the following:
template<class T>
class A
{
template<class X>
void foo(X);
};
Maybe we can use a single template argument for both?
template<class U>
void A<U>::foo(U u)
{
return;
}
error: out-of-line definition of 'foo' does not match any declaration in 'A<T>'
No, we cannot. Well, maybe like this?
template<class U>
void A<U>::foo<U>(U u)
{
return;
}
error: cannot specialize a member of an unspecialized template
No. And this?
template<class U, class V>
void A<U>::foo(V u)
{
return;
}
error: too many template parameters in template redeclaration
How about using a default to emulate the matching?
template<class U>
template<class V = U>
void A<U>::foo(V u)
{
return;
}
error: cannot add a default template argument to the definition of a member of a class template
Clearly, the compiler is worried about matching the declaration. That's because the compiler doesn't match template definitions to specific calls (as one might be used to from a functional language) but to the template declaration. (Code so far here).
So on a basic level, the answer is "because the template definition must match the template declaration". This still leaves open the question "why can we not just omit the class template parameters then?" (as far as I can tell no ambiguity for the template can exist so repeating the template parameters does not help) though...
Consider a function template declaration
tempalte <typename T>
void foo();
now a definition
void foo() { std::cout << "Hello World"; }
is either a specialization of the above template or an overload. You have to pick either of the two. For example
#include <iostream>
template <typename T>
void foo();
void foo() { std::cout << "overload\n"; }
template <typename T>
void foo() { std::cout << "specialization\n"; }
int main() {
foo();
foo<int>();
}
Prints:
overload
specialization
The short answer to your question is: Thats how the rules are, though if you could ommit the template <typename T> from a definition of the template, a different way would be required to define an overload.
Somehow I am not able to understand this answer.
When a member function is defined outside class, why can't we have a simple nested name specifier (for example Foo::Foo(){} instead of having a more verbose Foo<T>::Foo(){}), even after the template declaration is repeated right before the member function definition.
The subtlety is that in
template<class T>
int VisitedSet::getSize() {
return vec.size();
}
the C++ compiler would not know yet what T applies to. When you're working inside the class definition for VisitedSet, you can use VisitedSet::... unambiguously, but outside the does not work.
When you're outside the class definition, the syntax requires that you tell the compiler what the template parameters are using the tempate<class T> syntax, and then that you tell the compiler where they apply.
In this case, that may seem ambiguous, but consider the following nested case:
template<class T>
class Foo {
template<class R>
class Bar {
int getSize();
};
};
Then it's more obvious why you need to be specific:
template<class X, class Y>
int Foo<Y>::Bar<X>::getSize() { ...
and
template<class A, class B>
int Foo<A>::Bar<B>::getSize() { ...
are both valid.
So that's why you need
template<class T>
int VisitedSet<T>::getSize() {
return vec.size();
}
even in the simpler case shown in the answer you link to.
Let's say I have a class template:
template <typename T>
class Array {
...
int length() const;
};
The definition of length would be
template <typename T>
int Array<T>::length() const
{
...
}
But why wouldn't it be? (I)
int Array<T>::length() const
{
...
}
Or maybe: (II)
template <typename T>
int Array::length() const
{
...
}
I guess (II) would be a function template. But actually I cannot understand the logic behind this syntax. Any rules to understand templates syntax?
int Array<T>::length() const
{
...
}
Illegal if:
you have not declared a class called T
you have not used typedef to give an existing type a new name - T
ex:
class T;
typedef double T;
using T = double;
template <typename T>
int Array::length() const
{
...
}
Illegal if:
you don't have a class called Array - different from template <class T> Array
Why it can't be (I) is easy: Without the template line, the compiler would have no choice but to interpret the < as a less-than operator, which would definitely not result in a useful function definition.
For (II) we need to consider how you would represent a function template of a class template. Say your class looked like this:
template <typename T>
class Array {
...
template <typename U>
int length() const;
};
Now you need to be able to explicitly specify which component takes which template parameter. Without explicitly specifying the <T> and <U> you would have at minimum a bunch of confusion about which parameter applies to which template. At worst it would be ambiguous and uncompilable.
template <typename T>
int Array::length() const
There may be partial specializations of a template. How is the compiler supposed to know whether this is a definition for the member of the primary template or a partial specialization?
int Array<T>::length() const
Every name in C++ must be declared. T, if to be used as a template parameter, must also be declared as one. You haven't, therefore the compiler will look for an earlier declaration and issue an error message as he finds none.
can we declare a template function in a normal class with out a template class or is it should be always inside a template class ?
can we declare a template function in a normal class with out a template class
Yes we can. For example
class demo
{
public:
template <typename T>
void func(const T& x) {
//do stuffs
}
};
int main()
{
demo d;
d.func<int>(5);
}
is perfectly valid
Yes, you can have template functions in non-templated classes too, e.g.:
struct X {
template<class T>
void f(const T& t) {
// ...
}
};
Yes you can , but make sure your definition and declaration of the template function is in the header files. If you want to know more why is this like that or more about templates in general i can recommend you this book Templates - Complete Guide
I'm working with some generated classes with broken polymorphism. For every generated class T, there are a handful of T_type_info, T_writer, T_reader classes which are only related to T conceptually.
What I'm trying to do is something like this:
template <class T> class Wrapper
{
public:
template <class W> W topic_cast(BrokenBaseClassWriter* p);
// other operations with the same problem ...
};
template <> class Wrapper<MyTopic>
{
public:
template <> MyTopicWriter* topic_cast(BrokenBaseClassWriter* p) { ... }
};
So that I can do things like:
void Write(const Wrapper<T>& topic)
{
BrokenBaseClassWriter p = not_important;
topic.topic_cast(p)->do_stuff();
}
My T classes are generated from an IDL and are concepts that exist in application space. They don't derive from anything. In my example above, W is not really an independent parameter, it's "Something Not T that depends on T". I'm trying to keep all knowledge of T in the app, and all knowledge of T' (without knowing about T) in the backend.
The compiler however says my topic_cast function is not a template function - I think because the template occurs in the return type and it wouldn't be distinguishable from any other instantiations. I know that that (templates differ only by return type) is not legal. Only in my case it really would be unique because W is not an independent parameter. But arguing with the compiler is seldom helpful.
Can I do this, or is there another way to do this "cast as function of template type"?
Could this not be achieved with a traits system?
template <typename T> struct my_traits
{
};
template <> struct my_traits<MyClass>
{
typedef MyWriter writer_type;
};
template <typename T> struct Wrapper
{
typename my_traits<T>::writer_type topic_cast();
};
This can't work:
topic.topic_cast(p)->do_stuff();
Because the compiler can not deduce the return type.
So you have to explicitly tell the compiler what return type you want:
topic.topic_cast<MyType>(p)->do_stuff();
The implementation you provide is for a specific type.
So when you use that specific type that code will be produced:
topic.topic_cast<MyTopicWriter>(p)->do_stuff(); // Use the explicit specialization
This compiles with gcc:
class BrokenBaseClassWriter;
class MyTopic;
class MyTopicWriter;
template <class T> class Wrapper
{
public:
template <class W> W *topic_cast(BrokenBaseClassWriter* p);
// other operations with the same problem ...
};
template <> template<>
MyTopicWriter *Wrapper<MyTopic>::topic_cast<MyTopicWriter>(BrokenBaseClassWriter* p)
{
return 0;
}
int main(int argc, int argv)
{
BrokenBaseClassWriter* p = NULL;
Wrapper<MyTopic> caster;
MyTopicWriter *casted = caster.topic_cast<MyTopicWriter>(p);
}
Of course, it still exposes MyTopicWriter in your main code...