[英]Why is cout undefined in my cpp if it's included in my header?
I have a class Ah which has #include iostream我有一个类 Ah,它有 #include iostream
Then in Bh I #include Ah然后在 Bh I #include Ah
Then in Ch I #include Bh然后在 Ch I #include Bh
In C.cpp I am defining the print() function as在 C.cpp 中,我将 print() 函数定义为
void C::print() const {
cout << "C has specs of: " << getSpecs() << endl;
}
The cout is undefined for C.cpp why is that? C.cpp 的 cout 未定义,这是为什么?
I thought that since iostream was included in Ah and since Bh includes Ah and Ch includes Bh it would include iostream to be used in my C.cpp我认为,由于 iostream 包含在 Ah 中,并且 Bh 包含 Ah,Ch 包含 Bh,因此它将包含要在我的 C.cpp 中使用的 iostream
Is there anyway to accomplish this without #including iostream on all my .h's?有没有办法在我所有的 .h 文件上不使用 #include iostream 来实现这一点?
I'm pretty new to inheritance and learning how that works right now.我现在对继承和学习它的工作原理很陌生。
cout
is in the namespace std
. cout
在命名空间std
。 Use std::cout
and std::endl
instead of cout
and endl
.使用
std::cout
和std::endl
而不是cout
和endl
。
along with @fuzzything44's answer you could also use连同@fuzzything44的答案,你也可以使用
using namespace::std;
at the top of your .cpp file or even put it in a common header.在 .cpp 文件的顶部,甚至将它放在一个公共标题中。
The downside to doing that is that you junk-up the global namespace and could get ambiguous or conflicting terminals if you're including a lot of headers and doing a lot of using namespace:xyz;这样做的缺点是,如果您包含大量标头并使用 namespace:xyz;进行大量操作,则您会破坏全局命名空间,并且可能会出现歧义或冲突的终端; declarations;
声明;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.