在 c++ 运算符中使用友元函数访问私有成员时遇到问题<<过载

Trouble accessing private members with friend function in c++ operator<< overload

本文关键字:lt 成员 遇到 问题 过载 运算符 c++ 访问 函数 友元      更新时间:2023-10-16

所以我正在尝试重载<<运算符。从我能看到的所有来源来看,语法是正确的,但 eclipse 不喜欢它。

我收到几个错误:多项式::P rivateStruct* 多项式::head 是私有的

和:struct Polynomial::P rivateStruct 是私有的。

我想将此结构保密以隐藏实现细节。

std::ostream& operator<<(std::ostream& outputStream, Polynomial& rhs)
    {
        Polynomial::PrivateStruct *p = rhs.head;
        //implementation details
        return outputStream;
    }

和宣言:

friend std::ostream& operator<<(std::ostream& outputStream, const Polynomial& rhs);

声明和定义不匹配 - 一个引用const,另一个引用非常量。匹配它们,您就可以开始了。