2012-12-03 59 views
0

我有以下声明:类的朋友函数不能访问其私有向量;

friend ostream& operator<<(ostream&,const List&); 

,我有如下定义:

ostream& operator<<(ostream& out,const List& item) { 
    vector<List::Employee>::const_iterator It; 
    for (It=item.employees.begin();It!=item.employees.end();It++) {} 
} 

员工是我的一个结构自身和员工是员工的类的私人矢量名单。编译器给我以下错误:

std::vector<List::Employee,std::allocator<List::Employee>> List::employees is private 

任何想法如何解决它?

+5

哪里是朋友声明?记得友谊不是继承... –

+0

它是在类的公共方法 –

+0

A *方法* ??你的意思是类的decl(列表类,顺便说一句)? – WhozCraig

回答

0

朋友声明应该在List类定义中。

class List{ 
    ... 
    friend ostream& operator<<(ostream&,const List&); 
}; 
+0

很确定,当他说“......这是在List类中声明的时候,OP意味着什么”。 – WhozCraig