类的 friend 函数无法访问其私有向量;

friend function of the class can't access to its private vector;

本文关键字:向量 访问 friend 函数 类的      更新时间:2023-10-16

我有以下声明:

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

有什么想法如何解决吗?

friend 声明应该在 List 类定义中。

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