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