![](/img/trans.png)
[英]Why am I getting an “use of undefined type” error in my code, when I have the header for it included?
[英]Why is cout undefined in my cpp if it's included in my header?
我有一个类 Ah,它有 #include iostream
然后在 Bh I #include Ah
然后在 Ch I #include Bh
在 C.cpp 中,我将 print() 函数定义为
void C::print() const {
cout << "C has specs of: " << getSpecs() << endl;
}
C.cpp 的 cout 未定义,这是为什么?
我认为,由于 iostream 包含在 Ah 中,并且 Bh 包含 Ah,Ch 包含 Bh,因此它将包含要在我的 C.cpp 中使用的 iostream
有没有办法在我所有的 .h 文件上不使用 #include iostream 来实现这一点?
我现在对继承和学习它的工作原理很陌生。
cout
在命名空间std
。 使用std::cout
和std::endl
而不是cout
和endl
。
连同@fuzzything44的答案,你也可以使用
using namespace::std;
在 .cpp 文件的顶部,甚至将它放在一个公共标题中。
这样做的缺点是,如果您包含大量标头并使用 namespace:xyz;进行大量操作,则您会破坏全局命名空间,并且可能会出现歧义或冲突的终端; 声明;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.