小编nod*_*ddy的帖子

模板功能的前向声明

我有一个带有朋友模板功能的模板类.我目前有以下代码,它正在工作:

template<class T>
class Vector
{
  public:
    template<class U, class W>
    friend Vector<U> operator*(const W lhs, const Vector<U>& rhs);
}

template<class U, class W>
Vector<U> operator*(const W lhs, const Vector<U>& rhs)
{
  // Multiplication
}
Run Code Online (Sandbox Code Playgroud)

我希望我的解决方案能够向朋友函数进行前向声明,这样我就可以获得安全优势,并且与我当前的方法相比,它提供了一对一的通信.我尝试了以下但仍然遇到错误.

template<class T>
class Vector;

template<class T, class W>
Vector<T> operator*(const W lhs, const Vector<T>& rhs);

template<class T>
class Vector
{
  public:
    friend Vector<T> (::operator*<>)(const W lhs, const Vector<T>& rhs);
}

template<class T, class W>
Vector<T> operator*(const W lhs, const Vector<T>& rhs)
{
  // Multiplication …
Run Code Online (Sandbox Code Playgroud)

c++ templates friend

6
推荐指数
1
解决办法
4606
查看次数

标签 统计

c++ ×1

friend ×1

templates ×1