0

As the title says I want to overload the << operator with a template class

but keep getting a "does not match any template declaration"

eg. in the .h file

template <class T>
class foo
{
friend ostream & operator<< <T>(ostream &output , foo<T> & F);

}

and in the .cpp file

template <class T>
ostream & operator<< <T>(ostream &output , foo<T> & F)
{

output<<"whatever";

return output;
}

How would I possible do that as I'm still learning to this.

  • 1
    Unrelated, but do you really want to split it into a header and a cpp file? Normally, you put the definition in the headers when dealing with templates. – super Nov 17 '21 at 15:49
  • @super I have to as it is part of the spec that they gave me for this assignment – blackarrow Nov 17 '21 at 15:52
  • TL;DR: Do like [this](https://godbolt.org/z/qaMWexs1a) – Ted Lyngmo Nov 17 '21 at 15:58
  • Templates are like *core language macros*. They are instantiated when used. By putting the definition in a `*.cpp` file, the definition won't be automagically instantiated when used. That's why templates that are shared between translation units go in header files. (You can put the definition in a `*.cpp` file, but then you have to instantiate it explicitly. Which is a bother, and fragile.) – Eljay Nov 17 '21 at 16:22

0 Answers0