源码

c – friend声明声明一个非模板函数


参见英文答案 > overloading friend operator<< for template class????????????????????????????????????5个
我有一个基类类似于下面的代码.我试图重载<然而,g说:

base.h:24: warning: friend declaration ‘std::ostream& operator<<(std::ostream&, Base*)’ 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

我试过添加<>在<

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

base.cpp

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

我想在标题base.h中有这个或类似的东西:

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

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

我在网上其他地方读到过<>之间<

最佳答案

(0)

本文由 投稿者 创作,文章地址:https://blog.isoyu.com/archives/c-friendshengmingshengmingyigefeimobanhanshu.html
采用知识共享署名4.0 国际许可协议进行许可。除注明转载/出处外,均为本站原创或翻译,转载前请务必署名。最后编辑时间为:9月 26, 2019 at 05:02 上午

热评文章