【发布时间】:2013-04-22 09:17:13
【问题描述】:
为什么我不能对接受模板参数的友元函数使用相同的模板参数?我的意思是下面的代码没问题!
template <class Vertex>
class Edge
{
template <class T>
friend ostream& operator<<(ostream& os, const Edge<T>& e);
/// ...
};
template <class T>
ostream& operator<<(ostream& os, const Edge<T>& e)
{
return os << e.getVertex1() << " -> " << e.getVertex2();
}
但是这个不行。为什么?问题是什么? (我得到链接器错误。)
template <class Vertex>
class Edge
{
friend ostream& operator<<(ostream& os, const Edge<Vertex>& e);
/// ...
};
template <class T>
ostream& operator<<(ostream& os, const Edge<T>& e)
{
return os << e.getVertex1() << " -> " << e.getVertex2();
}
【问题讨论】:
-
一个朋友是模板,另一个不是。