繁体   English   中英

C++ 模板 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;
}

但它给了我这个错误我在互联网上尝试了一切

在 Couple.cpp:2 包含的文件中,来自 main.cpp:2:Couple.h:在 'class Couple<int, std::__cxx11::basic_string >' 的实例化中:main.cpp:7:28:需要来自这里 Couple.h:14:21: error: template-id 'operator<< <>' for 'std::ostream& operator<<(std::ostream&, Couple<int, std::__cxx11::basic_string >)'不匹配任何模板声明

需要提前声明算子模板。 例如

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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