:icon_question: Hi friends i have got a problem on the template class.
that is why do i need to declare the key word template<class t>
on every function defintion of the member function.

since we have declared it as global declaration on the top of the class.

>why do i need to declare the key word template<class t>
>on every function defintion of the member function.
If the definition is inline, you don't have to:

#include <iostream>

template <typename T>
class foo {
public:
  void bar()
  {
    std::cout<<"foo::bar\n";
  }
};

int main()
{
  foo<int> f;

  f.bar();
}

If the definition isn't inline you have to specify the template arguments so that they're visible to the declaration. When defining a member function outside of the class definition, you have to specify what class the member function is a member of:

class foo {
public:
  void bar();
};

void foo::bar()
{
  std::cout<<"foo::bar\n";
}

Notice the definition has foo:: , which is the fully qualified name of the type. You have to do the same thing with template classes, but the type of the class depends on the template arguments as well. For template <typename T> class foo; , the fully qualified name is foo<T>:

#include <iostream>

template <typename T>
class foo {
public:
  void bar();
};

template <typename T>
void foo<T>::bar()
{
  std::cout<<"foo::bar\n";
}

int main()
{
  foo<int> f;

  f.bar();
}

In this definition of bar, you need to define the template arguments (ie. T) for foo<T> to be well formed. The template arguments also have to be defined if you use them anywhere else in the definition.

This article has been dead for over six months. Start a new discussion instead.