运算符在模板中重载 [英] operator overloading in templates
问题描述
我在模板中添加了一个操作符overloader
class:
graph.h :
模板< class NODE>
class Node
{
朋友ostream&运营商LT;< (ostream&,Node< NODE>&); // 80行
NODE * info;
[snip]
}
图。 cpp:
[snip]
模板< class NODE>
ostream& operator<<(ostream& output,const Node< ; NODE>&节点)
{
输出<< *(node.info)<< " " ;;
返回输出;
}
[snip}
使用gcc编译时我收到警告:
来自graph.cpp的文件:2:
... / graph.h:80:警告:朋友声明`std :: ostream&
operator<<(std :: ostream&,Node< NODE>&)''声明一个
非模板函数
... /graph.h:80:警告:(如果这不是您的意图,请确保已经声明
功能模板并在
$ b $之后添加<> b函数名称在这里)-Wno-non-template-friend禁用此警告
警告的原因是什么,我该如何避免?
谢谢。
问候,
克里斯
Christian Christmann写道:我在模板中添加了一个操作符overloader
类:
graph.h:
在这里添加:
tem板< class NODE> class Node;
template< class NODE> ostream的和放;运算符<(&ostream&,Node< NODE>&);
并且,运营商<<<<<<<<<<<<<<<<<需要它第二个
参数作为对* non-const * Node<>?
模板的引用< class NODE>
类Node
{
朋友ostream& operator<< (ostream&,Node< NODE>&); //第80行
NODE * info;
[snip]
}
graph.cpp:
[snip]
template< class NODE>
ostream& operator<<(ostream& output,const Node< NODE>& node)
{
output<< *(node.info)<< " " ;;
返回输出;
}
[snip}
使用gcc编译时我收到警告:
在graph.cpp中包含的文件中:2:
../graph.h:80:警告:朋友声明`std :: ostream&
运算符<<(std :: ostream&,Node< NODE>&)''声明一个
非模板函数
../graph.h:80:警告:(如果这不是你想要的,请确保
函数模板已经声明并添加< ;>此后
函数名称)-Wno-non-template-friend禁用此警告
该警告的原因是什么?如何避免?
声明您的运营商<<作为类模板之前的模板。见上面的
。
V
" Victor Bazarov" <五******** @ comAcast.net>在消息中写道
news:s%******************* @ newsread1.mlpsca01.us.t o.verio.net ...Christian Christmann写道:我在模板中添加了一个操作符overloader
类:
graph.h:
在这里添加:
模板< class NODE> class Node;
template< class NODE> ostream的和放; operator<(&ostream&,Node< NODE>&);
为什么这些声明是必要的?我注意到VS 2003和VC ++都不需要它们。
template< class NODE>
class Node
朋友ostream& operator<< (ostream&,Node< NODE>&); //第80行
NODE * info;
[snip]
}
graph.cpp:
[snip]
template< class NODE>
ostream& operator<<(ostream& output,const Node< NODE>& node)
{
output<< *(node.info)<< " " ;;
返回输出;
}
[snip}
DW
David White写道:" Victor Bazarov" <五******** @ comAcast.net>在消息中写道
新闻:s%******************* @ newsread1.mlpsca01.us.t o.verio.net ...
Christian Christmann写道:
我在模板中添加了一个操作符overloader
类:
graph.h:
在此处添加:
模板< class NODE> class Node;
template< class NODE> ostream的和放; operator<(&ostream&,Node< NODE>&);
为什么这些声明是必要的?我注意到VS 2003和VC ++都不需要它们。
标准,在14.5.3 / 1中,如果朋友函数声明
不是模板声明(不是以''模板''
关键字开头),而且该函数的名称是模板ID,朋友
声明是指模板特化,如果它不是模板ID
而是''unqualified-id'',它声明了一个普通的函数。所以,如果你(或者OP
)不要声明运营商<<事先作为模板,它被认为是一个简单的函数,这违背了它的第二个
参数依赖于''NODE''这一事实。
如果''运算符<<''是模板ID,则解决冲突,通过声明''运算符<<'''模板来实现
这是''Node''上面的第二个模板
声明。声明''Node''作为类模板
(第一个声明语句)是必要的,因为它在
中用于以下函数模板声明。
template< class NODE>
class Node
朋友ostream& operator< ;< (ostream&,Node< NODE>&); //第80行
NODE * info;
[snip]
}
graph.cpp:
[snip]
template< class NODE>
ostream& operator<<(ostream& output,const Node< NODE>& node)
{
output<< *(node.info)<< " " ;;
返回输出;
}
[snip}
DW
>
V
Hi,
I added an operator overloader in my template
class:
graph.h:
template <class NODE>
class Node
{
friend ostream &operator<< (ostream &, Node<NODE> &); //line 80
NODE* info;
[snip]
}
graph.cpp:
[snip]
template <class NODE>
ostream &operator<<(ostream &output, const Node<NODE> &node)
{
output << *(node.info) << " ";
return ouput;
}
[snip}
While compiling with gcc I get the warning:
In file included from graph.cpp:2:
.../graph.h:80: warning: friend declaration `std::ostream&
operator<<(std::ostream&, Node<NODE>&)'' declares a
non-template function
.../graph.h:80: 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
What is the reason for that warning and how can I avoid it?
Thank you.
Regards,
Chris
Christian Christmann wrote:I added an operator overloader in my template
class:
graph.h:
Add here:
template<class NODE> class Node;
template<class NODE> ostream& operator<<(ostream&, Node<NODE>&);
And, is there any particular reason why your operator<< needs it second
argument as a reference to a *non-const* Node<>?
template <class NODE>
class Node
{
friend ostream &operator<< (ostream &, Node<NODE> &); //line 80
NODE* info;
[snip]
}
graph.cpp:
[snip]
template <class NODE>
ostream &operator<<(ostream &output, const Node<NODE> &node)
{
output << *(node.info) << " ";
return ouput;
}
[snip}
While compiling with gcc I get the warning:
In file included from graph.cpp:2:
../graph.h:80: warning: friend declaration `std::ostream&
operator<<(std::ostream&, Node<NODE>&)'' declares a
non-template function
../graph.h:80: 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
What is the reason for that warning and how can I avoid it?
Declare your operator<< as a template before the class template. See
above.
V
"Victor Bazarov" <v.********@comAcast.net> wrote in message
news:s%*******************@newsread1.mlpsca01.us.t o.verio.net...Christian Christmann wrote:I added an operator overloader in my template
class:
graph.h:
Add here:
template<class NODE> class Node;
template<class NODE> ostream& operator<<(ostream&, Node<NODE>&);
Why are these declarations necessary? I notice that neither VS 2003 nor VC++
6.0 needs them.
template <class NODE>
class Node
{
friend ostream &operator<< (ostream &, Node<NODE> &); //line 80
NODE* info;
[snip]
}
graph.cpp:
[snip]
template <class NODE>
ostream &operator<<(ostream &output, const Node<NODE> &node)
{
output << *(node.info) << " ";
return ouput;
}
[snip}
DW
David White wrote:"Victor Bazarov" <v.********@comAcast.net> wrote in message
news:s%*******************@newsread1.mlpsca01.us.t o.verio.net...Christian Christmann wrote:I added an operator overloader in my template
class:
graph.h:
Add here:
template<class NODE> class Node;
template<class NODE> ostream& operator<<(ostream&, Node<NODE>&);
Why are these declarations necessary? I notice that neither VS 2003 nor VC++
6.0 needs them.
The Standard, in 14.5.3/1, says that if the friend function declaration
that is not a template declaration (does not begin with the ''template''
keyword) and the name of the function is a template-id, the friend
declaration refers to a template specialisation, if it''s not a template-id
but an ''unqualified-id'', it declares an ordinary function. So, if you (or
the OP) don''t declare operator<< as a template beforehand, it is
considered a simple function, which goes against the fact that its second
argument depends on ''NODE''.
The conflict is resolved if ''operator<<'' is a template-id, which is
achieved by declaring ''operator<<'' a template (that''s the second template
declaration above ''Node''). The declaration of ''Node'' as a class template
(the very first declaration statement) is necessary because it''s used in
the following function template declaration.
template <class NODE>
class Node
{
friend ostream &operator<< (ostream &, Node<NODE> &); //line 80
NODE* info;
[snip]
}
graph.cpp:
[snip]
template <class NODE>
ostream &operator<<(ostream &output, const Node<NODE> &node)
{
output << *(node.info) << " ";
return ouput;
}
[snip}
DW
V
这篇关于运算符在模板中重载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!