1. <small id='KmkFr'></small><noframes id='KmkFr'>

      <tfoot id='KmkFr'></tfoot>

      <i id='KmkFr'><tr id='KmkFr'><dt id='KmkFr'><q id='KmkFr'><span id='KmkFr'><b id='KmkFr'><form id='KmkFr'><ins id='KmkFr'></ins><ul id='KmkFr'></ul><sub id='KmkFr'></sub></form><legend id='KmkFr'></legend><bdo id='KmkFr'><pre id='KmkFr'><center id='KmkFr'></center></pre></bdo></b><th id='KmkFr'></th></span></q></dt></tr></i><div id='KmkFr'><tfoot id='KmkFr'></tfoot><dl id='KmkFr'><fieldset id='KmkFr'></fieldset></dl></div>
        <bdo id='KmkFr'></bdo><ul id='KmkFr'></ul>

      1. <legend id='KmkFr'><style id='KmkFr'><dir id='KmkFr'><q id='KmkFr'></q></dir></style></legend>

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

        时间:2023-05-25
        <tfoot id='ms8Uo'></tfoot>
      2. <small id='ms8Uo'></small><noframes id='ms8Uo'>

          <bdo id='ms8Uo'></bdo><ul id='ms8Uo'></ul>
        • <i id='ms8Uo'><tr id='ms8Uo'><dt id='ms8Uo'><q id='ms8Uo'><span id='ms8Uo'><b id='ms8Uo'><form id='ms8Uo'><ins id='ms8Uo'></ins><ul id='ms8Uo'></ul><sub id='ms8Uo'></sub></form><legend id='ms8Uo'></legend><bdo id='ms8Uo'><pre id='ms8Uo'><center id='ms8Uo'></center></pre></bdo></b><th id='ms8Uo'></th></span></q></dt></tr></i><div id='ms8Uo'><tfoot id='ms8Uo'></tfoot><dl id='ms8Uo'><fieldset id='ms8Uo'></fieldset></dl></div>

              <tbody id='ms8Uo'></tbody>
              <legend id='ms8Uo'><style id='ms8Uo'><dir id='ms8Uo'><q id='ms8Uo'></q></dir></style></legend>

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

                  问题描述

                  限时送ChatGPT账号..

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

                  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++:友元声明‘声明一个非模板函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:C++ 重载函数作为模板参数 下一篇:使用 SFINAE 检查全局操作符<<?

                  相关文章

                  最新文章

                  <tfoot id='FmCCH'></tfoot>
                  <i id='FmCCH'><tr id='FmCCH'><dt id='FmCCH'><q id='FmCCH'><span id='FmCCH'><b id='FmCCH'><form id='FmCCH'><ins id='FmCCH'></ins><ul id='FmCCH'></ul><sub id='FmCCH'></sub></form><legend id='FmCCH'></legend><bdo id='FmCCH'><pre id='FmCCH'><center id='FmCCH'></center></pre></bdo></b><th id='FmCCH'></th></span></q></dt></tr></i><div id='FmCCH'><tfoot id='FmCCH'></tfoot><dl id='FmCCH'><fieldset id='FmCCH'></fieldset></dl></div>
                  <legend id='FmCCH'><style id='FmCCH'><dir id='FmCCH'><q id='FmCCH'></q></dir></style></legend>
                1. <small id='FmCCH'></small><noframes id='FmCCH'>

                      <bdo id='FmCCH'></bdo><ul id='FmCCH'></ul>