C++:友元声明‘声明一个非模板函数

2021-12-13 00:00:00 gcc templates c++ ostream

我遇到了重载 << 流运算符的问题,但我没有找到解决方案:

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&operator<<(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]

错误:'std::ostream&NVector::operator<<(std::ostream&, const NVector&)' 必须只取一个参数

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

如何解决这个问题?

非常感谢.

推荐答案

在你的代码中有两个不同的问题,第一个是 friend 声明(正如警告明确说的,也许不是这样清晰易懂)将单个非模板化函数声明为友元.也就是说,当您实例化模板 NVector 时,它声明了一个非模板化函数 std::ostream&operator<<(std::ostream&,NVector) 作为朋友.请注意,这与声明您作为朋友提供的模板函数不同.

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.

我建议您在类定义中定义友元函数.您可以在此答案中阅读更多相关信息.

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;
   }
};

或者,您可以选择其他选项:

Alternatively you can opt for other options:

  1. operator<< 模板声明为朋友(将授予对模板的任何和所有实例的访问权限),
  2. 将该模板的特定实例声明为朋友(编写起来更麻烦)或
  3. 完全避免友谊提供公共print(std::ostream&)成员函数并从非友元模板operator<<调用它.我仍然会选择与非模板函数交朋友,并在模板化类中提供定义.
  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;
};

相关文章