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

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)定义在类外面的情况怎么改,不求将函数实现放在类内部,谢谢

第1个回答  2013-05-04
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分钟。

追问

额,怎么发呀

追答

百度私信

追问

有时间帮忙解答一下么

追答

发过来把,或者你放在你的百度空间里面,我去看看

相似回答