Here's a test class for demonstrating what I mean.
#include <iostream>
template <typename T>
class Test
{
public:
int s;
friend std::istream& operator>>(std::istream& input, Test<T>& test1);
friend std::ostream& operator<<(std::ostream& output, const Test<T>& test1);
};
template <typename T>
std::istream& operator>>(std::istream& input, Test<T>& test1)
{
input >> test1.s;
return input;
}
template <typename T>
std::ostream& operator<<(std::ostream& output, const Test<T>& test1)
{
output << test1.s;
return output;
}
int main()
{
Test<int> clasaTest;
std::cin >> clasaTest;
std::cout << clasaTest;
}
If I rewrite it like this
template <typename T>
class Test
{
public:
int s;
template <typename U>
friend std::istream& operator>>(std::istream& input, Test<U>& test1);
template <typename U>
friend std::ostream& operator<<(std::ostream& output, const Test<U>& test1);
};
it will work properly, but I can't really understand why.
Shouldn't it work with the same T declared before? Because the class will have the same generic argument?