<tfoot id='mUtU7'></tfoot>

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

      <small id='mUtU7'></small><noframes id='mUtU7'>

    1. 友元声明声明了一个非模板函数

      时间:2023-05-24
      <i id='PEpxJ'><tr id='PEpxJ'><dt id='PEpxJ'><q id='PEpxJ'><span id='PEpxJ'><b id='PEpxJ'><form id='PEpxJ'><ins id='PEpxJ'></ins><ul id='PEpxJ'></ul><sub id='PEpxJ'></sub></form><legend id='PEpxJ'></legend><bdo id='PEpxJ'><pre id='PEpxJ'><center id='PEpxJ'></center></pre></bdo></b><th id='PEpxJ'></th></span></q></dt></tr></i><div id='PEpxJ'><tfoot id='PEpxJ'></tfoot><dl id='PEpxJ'><fieldset id='PEpxJ'></fieldset></dl></div>
            <tbody id='PEpxJ'></tbody>

            <tfoot id='PEpxJ'></tfoot>

            <legend id='PEpxJ'><style id='PEpxJ'><dir id='PEpxJ'><q id='PEpxJ'></q></dir></style></legend>

            <small id='PEpxJ'></small><noframes id='PEpxJ'>

              • <bdo id='PEpxJ'></bdo><ul id='PEpxJ'></ul>
                本文介绍了友元声明声明了一个非模板函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                限时送ChatGPT账号..

                我有一个类似于下面代码的基类.我试图重载 <<与 cout 一起使用.但是,g++ 说:

                I have a base Class akin to the code below. I'm attempting to overload << to use with cout. However, g++ is saying:

                base.h:24: warning: friend declaration ‘std::ostream& operator<<(std::ostream&, Base<T>*)’ declares a non-template function
                base.h:24: warning: (if this is not what you intended, make sure the function template has already been declared and add <> after the function name here) -Wno-non-template-friend disables this warning
                

                我试过在 << 之后添加 <>在类声明/原型中.但是,然后我得到它不匹配任何模板声明.我一直在尝试将运算符定义完全模板化(我想要),但我只能使用以下代码使其与手动实例化运算符一起工作.

                I've tried adding <> after << in the class declaration / prototype. However, then I get it does not match any template declaration. I've been attempting to have the operator definition fully templated (which I want), but I've only been able to get it to work with the following code, with the operator manually instantiated.

                base.h

                template <typename T>
                class Base {
                  public:
                    friend ostream& operator << (ostream &out, Base<T> *e);
                };
                

                base.cpp

                ostream& operator<< (ostream &out, Base<int> *e) {
                    out << e->data;
                return out;
                }
                

                我只想在标题 base.h 中包含此或类似内容:

                I want to just have this or similar in the header, base.h:

                template <typename T>
                class Base {
                  public:
                    friend ostream& operator << (ostream &out, Base<T> *e);
                };
                
                template <typename T>
                ostream& operator<< (ostream &out, Base<T> *e) {
                    out << e->data;
                return out;
                }
                

                我在网上的其他地方读到过将 <> 放在 << 之间.原型中的和 () 应该解决这个问题,但事实并非如此.我可以将其放入单个函数模板中吗?

                I've read elsewhere online that putting <> between << and () in the prototype should fix this, but it doesn't. Can I get this into a single function template?

                推荐答案

                听起来你想改变:

                friend ostream& operator << (ostream& out, const Base<T>& e);
                

                致:

                template<class T>
                friend ostream& operator << (ostream& out, const Base<T>& e);
                

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

                上一篇:如何在 C++ 中将 typename T 转换为字符串 下一篇:C++ 模板函数在头文件中编译但不是实现

                相关文章

                最新文章

                  • <bdo id='Rg4c0'></bdo><ul id='Rg4c0'></ul>

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

                    <small id='Rg4c0'></small><noframes id='Rg4c0'>

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