如果 cout 包含在我的标头中,为什么在我的 cpp 中未定义?

Why is cout undefined in my cpp if it's included in my header?

本文关键字:我的 为什么 cpp 未定义 cout 包含 如果      更新时间:2023-10-16

我有一个类A.h,它有#include iostream

则在B.h I #中包含A.h

则在C.h中包含B.h

在C.cpp中,我将print()函数定义为
void C::print() const {
cout << "C has specs of: " << getSpecs() << endl;
} 

C.cpp的计数是未定义的,为什么?

我认为,既然iostream包含在A.h中,既然B.h包含A.h,而C.h包含B.h,那么在我的c.p中应该包含iostream。

有没有办法完成这个没有#包括iostream在我所有的。h ?

cout在命名空间std中。用std::coutstd::endl代替coutendl

与@fuzzything44的答案一起使用

using namespace::std;

放在你的。cpp文件的顶部,或者甚至把它放在一个通用的头文件中。

这样做的缺点是,如果你包含了很多头文件,并使用namespace:xyz;声明;

做了很多,那么你可能会得到不明确或冲突的终端。