友元声明声明一个非模板函数
我有一个类似于下面代码的基类。我正在尝试超载<<与 cout 一起使用。 然而,g++ 说:
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
我尝试添加 <>之后<<在类声明/原型中。但是,然后我发现与任何模板声明都不匹配
。我一直在尝试将运算符定义完全模板化(这是我想要的),但我只能让它与以下代码一起使用,并手动实例化运算符。
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:
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;
}
我在网上其他地方读过,将 <>之间 <<原型中的 and () 应该可以解决这个问题,但事实并非如此。我可以将其放入单个函数模板中吗?
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;
}
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您可能正在寻找的是:
这只是模板的一个实例,其中运算符的模板参数与类的模板参数相匹配。
更新:不幸的是,这是非法的。 MSVC 和 Comeau 都拒绝了它。这就提出了一个问题:为什么原始错误消息几乎完全建议采用这种方法。
Probably what you are looking for is:
This friends only a single instantiation of the template, the one where the operator's template parameter matches the class's template parameter.
UPDATE: Unfortunately, it's illegal. Both MSVC and Comeau reject it. Which raises the question of why the original error message suggested pretty much EXACTLY this approach.
更改
为
应该也可以——我刚刚以这种方式解决了同样的问题。
changing
to
should work as well--I just solved an identical problem in this way.
更改
为
changing
To
听起来您想更改:
至:
It sounds like you want to change:
To:
Gcc 警告您是正确的。尽管它的外观(它采用 Base 参数),但它不是函数模板。
您的类定义具有友元函数的非模板声明(没有模板),但稍后的友元函数定义是函数模板(即以模板开头..)。
还有您的运营商<<需要一个 Base *。这是不正确的。它应该是 Base const &保留其内置语义
您可能正在查看如下内容:
如果您想要完全模板化,那么这可能就是您想要的。但我不确定这比前一个有用多少。由于查找涉及 ADL,因此您将永远无法解析 T 不等于 U 的任何条件(只要调用来自与此类不相关的上下文,例如来自“main”函数)
Gcc is rightly warning you. Despite it's appearances (it takes Base argument), it is not a function template.
Your class definition has a non-template declaration of the friend function (without the template), but the friend function definition later on is a function template (i.e. starts with template..).
Also your operator<< takes a Base *. This is not correct. It should be Base const & to retain it's built-in semantics
Probably you are looking at something as below:
If you want fully templated, then this is probably what you want. But I am not sure how much useful this is over the previous one. Since the lookup involves ADL, you will never be able to resolve to any condition where T is not equal to U (as long as the call is from a context not related to this class e.g. from 'main' function)