C++中,怎么在友元类中定义一个指向嵌套类的指针 20

template<classT>ostream&operator<<(ostream&os,constList<T>&list);template<classT>clas... template<class T>
ostream& operator<< (ostream& os,const List<T>& list);

template<class T>
class List {
public:
// 构造、析构、拷贝构造、拷贝赋值
List (void);
~List (void);
List (const List& that);
List& operator= (const List& that);
// 获取首元素
T& front (void);
const T& front (void) const;
// 向首部压入
void push_front (const T& data);
// 从首部弹出
void pop_front (void);
// 获取尾元素
T& back (void);
const T& back (void) const ;
// 向尾部压入
void push_back (const T& data) ;
// 从尾部弹出
void pop_back (void);
// 删除所有匹配的元素
void remove (const T& data);
// 清空
void clear (void);
// 判空
bool empty (void) const ;
// 大小
size_t size (void) const;
// 输出
friend ostream& operator<< <T>(ostream& os,const List<T>& list);
private:
// 节点模板
class Node {
public:
Node (const T& data, Node* prev = NULL,
Node* next = NULL) :
m_data (data), m_prev (prev),
m_next (next) {}
friend ostream& operator<< (ostream& os,const Node& node) {
return os << '(' << node.m_data << ')';
}
T m_data; // 数据
Node* m_prev; // 前指针
Node* m_next; // 后指针
};
bool equal (const T& a, const T& b);
Node* m_head; // 头指针
Node* m_tail; // 尾指针
};
template<class T>
ostream& operator<< (ostream& os,const List<T>& list) {
for (Node* //编译的时候这个地方报错,报错为‘Node’在此作用域中尚未声明,该怎么改node = list.m_head; node;node = node -> m_next)
os << *node;
return os;
}
只求将模板友元函数ostream& operator<< (ostream& os,const List<T>& list)定义在类外面的情况怎么改,不求将函数实现放在类内部,谢谢
展开
 我来答
chujiangke001
2013-05-04 · TA获得超过1605个赞
知道小有建树答主
回答量:1352
采纳率:0%
帮助的人:682万
展开全部
ostream& operator<< (ostream& os,const List<T>& list) {
for (Node* //编译的时候这个地方报错,报错为‘Node’在此作用域中尚未声明,该怎么改node = list.
的原因是声明出错了
::::::::::::::::::::::::

这里
bool empty (void) const ;
// 大小
size_t size (void) const;
// 输出
friend ostream& operator<< <T>(ostream& os,const List<T>& list);
改为
lllllllllllllllllllllllllllllllllllllllllllll
友元函数不是class里面的
template<class Y>
friend ostream& operator<< (ostream& os,const List<Y>& list);

你可以看看我的这个博客研究过。
http://blog.csdn.net/chujiangkedejiushu/article/details/8735958
注意 重载<< 的声明。
欢迎追问。
更多追问追答
追问
编译一样报错,
grant@grant:~/programc++$ g++ test.cpp
test.cpp: 在函数‘std::ostream& operator&)’中:
test.cpp:206:8: 错误: ‘Node’在此作用域中尚未声明
test.cpp:206:14: 错误: ‘node’在此作用域中尚未声明
追答
把你的代码发给我 我来调试下,我再等5分钟。
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式