模板类中的C ++重载operator + [英] C++ Overloading operator+ in a template class

查看:84
本文介绍了模板类中的C ++重载operator +的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

template<typename T>
class Matrix
{

    template<typename U>
    friend
    Matrix<U> operator+(const Matrix<U>& a, const Matrix<U>& b);


protected:

    size_t _m, _n;
    T *_alloc;
};

template<typename U>
Matrix<U> operator+(const Matrix<U>& a, const Matrix<U>& b)
{
    if(a._m == b._m && a._n == b._n)
    {
        Matrix<U> ret(a._m, a._n);

        // ...

        return ret;
    }
    else
    {
        throw "Matrix dimension mismatch error";
    }
}

在使用非模板类没有问题之前,我已经重载了operator+.在这里,我正在使用模板类.

I've overloaded operator+ before without problem using non-template classes. Here I'm using a template class.

Matrix<U> Matrix<U>::operator+(const Matrix<U>&, const Matrix<U>&)必须采用零或一个参数.

Matrix<U> Matrix<U>::operator+(const Matrix<U>&, const Matrix<U>&) must take either zero or one argument.

似乎编译器忽略了friend关键字.

It appears that the compiler is ignoring the friend keyword.

我也尝试过

friend
template<typename U>
Matrix<U> operator+(const Matrix<U>& a, const Matrix<U>& b);

但这给了我一个不同的编译器错误.

But this gives me a different compiler error.

expected unqualified-id before 'template'

如何用模板类重载operator+?

推荐答案

您可以使用成员函数或非成员函数重载+运算符.

You can overload the + operator using a member function or a non-member function.

当它是成员函数时,运算符的LHS是将在其上调用该函数的对象,而运算符的RHS是该函数的参数.因此,成员函数的唯一参数将是RHS.

When it is a member function, the LHS of the operator is the object on which the function will be called and RHS of the operator is the argument to the function. Hence, the only argument to the member function will be the RHS.

当它是成员函数时,运算符的LHS是该函数的第一个参数,运算符的RHS是该函数的第二个参数.

When it is a member function, the LHS of the operator is the first argument of the to the function and RHS of the operator is the second argument to the function.

成员功能

template<typename T>
class Matrix
{
  Matrix operator+(const Matrix& rhs) const {
    ...
  }
};

如果要在类定义之外实现它,可以使用:

If you want to implement it outside the class definition, you can use:

template<typename T>
  Matrix<T> Matrix<T>::operator+(const Matrix& rhs) const {
    ...
  }

非成员功能

template<typename T>
class Matrix
{
  Matrix operator+(const Matrix& lhs, const Matrix& rhs) {
    ...
  }
};

如果要在类定义之外实现它,则需要添加一些前向声明代码:

If you want to implement it outside the class definition, you need to add some forward declaration code:

// Forward declare the class template
template <typename T> class Matrix;

// Declare the function
template <typename T> Matrix<T> operator+(const Matrix<T>& lhs, const Matrix<T>& rhs);

// Declare the friend in the class definition
template <typename T>
class Matrix
{
   friend Matrix operator+<T>(const Matrix& lhs, const Matrix& rhs);
   //                     ^^^^ 
   // This makes operator+<int> a friend of Matrix<int>, not a friend
   // of Matrix<double>
};

然后实现功能

template <typename T> Matrix<T> operator+(const Matrix<T>& lhs, const Matrix<T>& rhs)
{
    ...
}

通过此设置,oprator+<int>仅是Matrix<int>的朋友,而不是Matrix<double>friend.

With this setup, oprator+<int> is a friend of Matrix<int> only, not a friend of Matrix<double>.

如果您使用

template <typename U>
friend
Matrix<U> operator+(const Matrix<U>& a, const Matrix<U>& b);

然后,operator+的所有实例都是Matrix的所有实例的朋友,而您不需要.

then, all instantiations of operator+ are friends of all instantiations of Matrix, which you don't need.

更新

示例工作代码:

#include <iostream>

// Forward declare the class template
template<typename T> class Matrix;

// Declare the function
template <typename T> Matrix<T> operator+(const Matrix<T>& lhs, const Matrix<T>& rhs);

// Declare the friend in the class definition
template <typename T>
class Matrix
{
   friend Matrix operator+<T>(const Matrix& lhs, const Matrix& rhs);
   //                     ^^^^ 
   // This makes operator+<int> a friend of Matrix<int>, not a friend
   // of Matrix<double>
};

template <typename T> Matrix<T> operator+(const Matrix<T>& lhs, const Matrix<T>& rhs)
{
   return Matrix<T>{};
}

int main()
{
   Matrix<int> a;
   Matrix<int> b;
   Matrix<int> c = a + b;
}

这篇关于模板类中的C ++重载operator +的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆