Hi I'm trying to overload the << operator
#include <iostream>
using namespace std;
template <class T, class U>
class Couple
{
public:
T cle;
U valeur;
public:
Couple();
Couple(T, U);
friend ostream &operator<<<>(ostream &, Couple<T, U>);
};
template <class T, class U>
ostream &operator<<(ostream &os, Couple<T, U> Cpl)
{
os << Cpl.cle << " : " << Cpl.valeur << endl;
return os;
}
but it's giving me this error I tried everything on the internet
In file included from Couple.cpp:2, from main.cpp:2: Couple.h: In instantiation of 'class Couple<int, std::__cxx11::basic_string >': main.cpp:7:28: required from here Couple.h:14:21: error: template-id 'operator<< <>' for 'std::ostream& operator<<(std::ostream&, Couple<int, std::__cxx11::basic_string >)' does not match any template declaration
You need to declare the operator template in advance. e.g.
// forward declaration for class template
template <class T, class U>
class Couple;
// declaration
template <class T, class U>
ostream &operator<<(ostream &os, Couple<T, U> Cpl);
template <class T, class U>
class Couple
{
...
// friend declaration
friend ostream &operator<<<>(ostream &, Couple<T, U>);
};
// definition
template <class T, class U>
ostream &operator<<(ostream &os, Couple<T, U> Cpl)
{
...
}