简体   繁体   English

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

[英]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::coutstd::endl而不是coutendl

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.

相关问题 当包含标题时,为什么在代码中出现“使用未定义类型”错误? - Why am I getting an “use of undefined type” error in my code, when I have the header for it included? 我是否在我的cpp文件或包含的头文件中放入#include指令是否重要? - Does it matter whether I put an #include directive in my cpp file or in an included header file? 当包含类头文件时,为什么在main中未定义我的数组? - Why is my array undefined in main when the class headers are included? 为什么 my.cpp 文件无法识别包含在 .h 文件中的变量? - Why is my .cpp file can't recognize variables from included in .h file? 我需要在我的主文件和头文件cpp文件中使用向量类……什么是正确的协议? - I need to use the vector class in my main and in my header cpp file … What's the proper protocol? 为什么我的 main.cpp 不打印头文件中的任何内容 - Why does my main.cpp not print anything from my header file 为什么我的IDE告诉我在Squirtle.cpp构造函数中未定义“ newHp”? - Why does my IDE tell me that “newHp” is undefined in my Squirtle.cpp constructor? 为什么我的cout打印两次? - Why does my cout print twice? 为什么我的 cout 输出没有立即出现? - Why does my cout output not appear immediately? 为什么我的嵌套 If 语句没有生成 cout? - Why my nested If statement is not generating cout?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM