【问题标题】:Template Friend Function of a Template Class (Friend with More Parameters)模板类的模板好友功能(参数较多的好友)
【发布时间】:2021-10-25 19:21:30
【问题描述】:

我无法让它工作:

template<std::size_t s, typename T>
class A;

template<std::size_t s, typename T, typename U>
A<s, T> operator *(U const lhs, A<s, T> const& rhs);

template<std::size_t s, typename T>
class A
{
    // Blabla
    
    template<typename U>
    friend A<s, T> operator * <>(U const lhs, A<s, T> const& rhs);
};

编译失败并显示以下错误消息:

在主模板的声明中使用了无效的模板 ID 'operator * '。

【问题讨论】:

  • 为什么&lt;&gt; 在类内operator* 之后?你到底想做什么?
  • @cigien 这是我的灵感:(web.mst.edu/~nmjxv3/articles/templates.html) 它适用于其他方法。
  • @cigien 实际上你是对的,在`operator *``之后不需要&lt;&gt;

标签: c++ templates friend


【解决方案1】:

由于多了一个模板参数(相对于模板类参数),似乎不需要在函数名后面加上&lt;&gt;。 总之,这对我来说效果很好:

template<std::size_t s, typename T>
class A;

template<std::size_t s, typename T, typename U>
A<s, T> operator *(U const lhs, A<s, T> const& rhs);

template<std::size_t s, typename T>
class A
{
    // Blabla
    
    template<typename U>
    friend A<s, T> operator *(U const lhs, A<s, T> const& rhs);
};

template<std::size_t s, typename T, typename U>
A<s, T> operator *(U const lhs, A<s, T> const& rhs)
{
    //BlaBla
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-03-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-12
    相关资源
    最近更新 更多