【问题标题】:use of flow operators on objects在对象上使用流运算符
【发布时间】:2021-10-27 23:21:07
【问题描述】:

我在使用运算符在对象上注入输入/输出流时遇到问题(运算符

-功能码:

ostream &operator<<( ostream &flux, Duree const& duree)
{
duree.afficher(flux) ; // <- Changement ici
return flux;
}

-方法代码:

void Duree::afficher(ostream &flux) const
{
flux << m_heures << "h" << m_minutes << "m" << m_secondes << "s";
}

但问题是,当我编译时,我被告知“ostream 不是类型名称”,“ostream 未在此代码中声明”。 我不明白,但我搜索了互联网,显然这是正确的做法。我正在使用 20.3 版的 Code :: Blocks。

【问题讨论】:

  • 你有#include &lt;iostream&gt;吗?
  • 是的,我做到了。
  • 您是#include &lt;iostream&gt; 还是using namespace std?或者你需要写std::ostream
  • 在前面弹出一个std::,看看显式命名是否能解决问题。如果不是,我们需要minimal reproducible example 来帮助您。
  • @Gouneken 如果我们按原样采用您发布的代码,我们会遇到很多错误。编译器错误必须伴随着正在编译的确切代码,否则我们会猜测问题所在。 This compiles with no errors。获取该代码,更改它以复制您看到的错误。

标签: c++ class ostream


【解决方案1】:

为此,函数必须在主文件中,而不是在类文件中。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-03-17
    • 2020-10-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多