简体   繁体   中英

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

I have a class Ah which has #include iostream

Then in Bh I #include Ah

Then in Ch I #include Bh

In C.cpp I am defining the print() function as

void C::print() const {
cout << "C has specs of: " << getSpecs() << endl;
} 

The cout is undefined for C.cpp why is that?

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

Is there anyway to accomplish this without #including iostream on all my .h's?

I'm pretty new to inheritance and learning how that works right now.

cout is in the namespace std . Use std::cout and std::endl instead of cout and endl .

along with @fuzzything44's answer you could also use

using namespace::std;

at the top of your .cpp file or even put it in a common header.

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; declarations;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM