简体   繁体   中英

C++ template-id does not match any template

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. eg

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM