C++ 如何在其他类中重载运算符==

C++ 如何在其他类中重载运算符==,c++,templates,overloading,C++,Templates,Overloading,我有一个甲级 在模板类B中 template<class key> class B 编译结果:参数太多 二, 当我尝试使用运算符时,编译结果:找不到运算符您可以为密钥类型定义嵌套的专用包装: template<class key> class B { struct EKey { key k; friend bool operator==(const EKey&, const EKey&) { return fals

我有一个甲级

在模板类B中

template<class key>
class B
编译结果:参数太多

二,


当我尝试使用运算符时,编译结果:找不到运算符

您可以为密钥类型定义嵌套的专用包装:

template<class key>
class B
{
    struct EKey {
        key k;
        friend bool operator==(const EKey&, const EKey&) { return false; }
    };
    // ...
};
模板
B类
{
结构EKey{
键k;
friend bool运算符==(const EKey&,const EKey&){return false;}
};
// ...
};

完整演示:

在私有名称空间中定义运算符,然后在
B
的成员函数定义中使用它,怎么样?@Quentin考虑过这个解决方案,但我认为它不够优雅
friend operator==(const key &a, const key &b)
template<class key>
class B
{
    struct EKey {
        key k;
        friend bool operator==(const EKey&, const EKey&) { return false; }
    };
    // ...
};