【问题标题】:Friend ostream operator << wont acces private fields朋友 ostream 运算符 << 无法访问私有字段
【发布时间】:2015-12-16 00:14:09
【问题描述】:

它只是不会访问私有字段

.hpp:
private:
    std::string model;

public:
    Piekarnik &operator << (const Ciasto &ciasto);
    friend std::ostream &operator<<(std::ostream &os, const Ciasto &ciasto);


.cpp:
Piekarnik &Piekarnik::operator<<(const Ciasto &ciasto)
{
    append(ciasto);
    return *this;
}
ostream &operator<<(ostream &os, const Piekarnik &piekarnik) // dwuargumentowy;
{
      os<<"Piekarnik (model: "<<piekarnik.model<<", temperatura: " << piekarnik.obecnyProgram.temperatura <<", pieczenie: " << EnumToString(piekarnik.obecnyProgram.pieczenie) << ", wlozone ciasto: " << EnumToString(piekarnik.wlozoneCiasto.rodzaj);
      return os<<")";
}

错误:'std::string Piekarnik::model' 是私有的

【问题讨论】:

  • 检查您的声明和定义签名,它不匹配。这是声明的friend std::ostream &amp;operator&lt;&lt;(std::ostream &amp;os, const Ciasto &amp;ciasto);

标签: private operator-keyword friend ostream


【解决方案1】:

将您的朋友声明更改为

friend std::ostream &operator<<(std::ostream &os, const Piekarnik&);

【讨论】:

  • 成功了!再上一堂课,同样的问题,我自己试试吧
  • 错误:'unsigned int Program::temperatura' 是私有的|它在另一个类程序中,我应该在那里输入什么才能使其完美运行?
  • Program 类中使用getter 或将相同的朋友声明放入Program 类中,或者将其标记为公共成员
  • 同一个朋友声明:Piekarnik 未定义(毫无疑问)好的,谢谢
  • Piekarnik的转发声明
猜你喜欢
  • 2015-08-03
  • 2021-05-29
  • 1970-01-01
  • 2016-11-02
  • 1970-01-01
  • 2020-09-06
  • 1970-01-01
  • 2019-08-08
  • 1970-01-01
相关资源
最近更新 更多