C ++:friend declaration'声明一个非模板函数 [英] C++ : friend declaration ‘declares a non-template function

查看:3266
本文介绍了C ++:friend declaration'声明一个非模板函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题,重载<< 流运算符,我找不到解决方案:

I have a problem to overload the << stream operator and I don't find the solution :

template<class T, unsigned int TN>
class NVector
{
    inline friend std::ostream& operator<< (
        std::ostream &lhs, const NVector<T, TN> &rhs);
};

template<class T, unsigned int TN>
inline std::ostream& NVector<T, TN>::operator<<(
    std::ostream &lhs, const NVector<T, TN> &rhs)
{
    /* SOMETHING */
    return lhs;
};

会产生以下错误讯息:


警告:朋友声明std :: ostream& <<<<(std :: ostream& const NVector&)'声明一个非模板函数[-Wnon-template-friend]

warning : friend declaration ‘std::ostream& operator<<(std::ostream&, const NVector&)’ declares a non-template function [-Wnon-template-friend]

:ostream& NVector :: operator<<<<(std :: ostream& const NVector&)必须只有一个参数

error: ‘std::ostream& NVector::operator<<(std::ostream&, const NVector&)’ must take exactly one argument

那个问题?

非常感谢。

推荐答案

你的代码中有两个不同的问题,第一个是朋友声明(警告清楚地说,也许不是那么清楚明白)声明一个单一的非模板函数作为朋友。也就是说,当你实例化模板 NVector< int,5> 时,它声明一个非模板函数 std :: ostream& operator<<<(std :: ostream&,NVector< int,5>)注意,这不同于声明您作为朋友提供的模板函数。

There are two different issues in your code, the first is that the friend declaration (as the warning clearly says, maybe not so clear to understand) declares a single non-templated function as a friend. That is, when you instantiate the template NVector<int,5> it declares a non-templated function std::ostream& operator<<(std::ostream&,NVector<int,5>) as a friend. Note that this is different from declaring the template function that you provided as a friend.

我建议您在类定义中定义friend函数。您可以在此回答中阅读更多内容。

I would recommend that you define the friend function inside the class definition. You can read more on this in this answer.

template <typename T, unsigned int TN>
class NVector {
   friend std::ostream& operator<<( std::ostream& o, NVector const & v ) {
      // code goes here
      return o;
   }
};

也可以选择其他选项:


  1. 运算符< 模板声明为朋友(将授予对模板的任何实例化的访问权限),

  2. 将该模板的特定实例化为朋友(更麻烦地写)或

  3. 避免友谊提供公共 :: ostream&)成员函数并从非朋友模板运算符< 调用它。我仍然可以选择在模板类中提供非模板函数和定义。

  1. declare the operator<< template as a friend (will grant access to any and all instantiations of the template),
  2. declare a particular instantiation of that template as a friend (more cumbersome to write) or
  3. avoid friendship altogether providing a public print( std::ostream& ) member function and calling it from a non-friend templated operator<<. I would still opt to befriend the non-template function an provide the definition inside the templated class.

第二个问题是,当你想要在左侧参数的类之外定义一个运算符,运算符是一个自由函数(不绑定到类),因此它不应该限定:

The second issue is that when you want to define an operator outside of the class of the left hand side argument, the operator is a free function (not bound to a class) and thus it should not be qualified:

template<class T, unsigned int TN>
inline std::ostream& operator<<(std::ostream &lhs, const NVector<T, TN> &rhs)
{
    /* SOMETHING */
    return lhs;
};

这篇关于C ++:friend declaration'声明一个非模板函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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