C++ template-id 不匹配任何模板

C++ template-id does not match any template

嗨,我正在尝试重载 << 运算符

#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;
}

但它给了我这个错误我在互联网上尝试了所有方法

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

您需要提前声明运算符模板。例如

// 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)
{
    ...
}