![](/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.