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

2023-03-10C/C++开发问题
56

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

问题描述

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

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

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

The End

相关推荐

无法访问 C++ std::set 中对象的非常量成员函数
Unable to access non-const member functions of objects in C++ std::set(无法访问 C++ std::set 中对象的非常量成员函数)...
2024-08-14 C/C++开发问题
17

从 lambda 构造 std::function 参数
Constructing std::function argument from lambda(从 lambda 构造 std::function 参数)...
2024-08-14 C/C++开发问题
25

STL BigInt 类实现
STL BigInt class implementation(STL BigInt 类实现)...
2024-08-14 C/C++开发问题
3

使用 std::atomic 和 std::condition_variable 同步不可靠
Sync is unreliable using std::atomic and std::condition_variable(使用 std::atomic 和 std::condition_variable 同步不可靠)...
2024-08-14 C/C++开发问题
17

在 STL 中将列表元素移动到末尾
Move list element to the end in STL(在 STL 中将列表元素移动到末尾)...
2024-08-14 C/C++开发问题
9

为什么禁止对存储在 STL 容器中的类重载 operator&amp;()?
Why is overloading operatoramp;() prohibited for classes stored in STL containers?(为什么禁止对存储在 STL 容器中的类重载 operatoramp;()?)...
2024-08-14 C/C++开发问题
6