2013-10-12 60 views
1

我在下面列出了我的代碼。我得到很多錯誤,說cout和endl沒有在這個範圍內聲明。我不知道自己做錯了什麼,或者如何強迫班級認出cout?我希望我正確地解釋我的問題。如果我註釋掉這些方法(不是構造函數),它就可以工作。我可能只是在這裏犯新手錯誤 - 請幫忙。Cout和endl錯誤

using namespace std; 

class SignatureDemo{ 
public: 
    SignatureDemo (int val):m_Val(val){} 
    void demo(int n){ 
     cout<<++m_Val<<"\tdemo(int)"<<endl; 
    } 
    void demo(int n)const{ 
     cout<<m_Val<<"\tdemo(int) const"<<endl; 
    } 
    void demo(short s){ 
     cout<<++m_Val<<"\tdemo(short)"<<endl; 
    } 
    void demo(float f){ 
     cout<<++m_Val<<"\tdemo(float)"<<endl; 
    } 
    void demo(float f) const{ 
     cout<<m_Val<<"\tdemo(float) const"<<endl; 
    } 
    void demo(double d){ 
     cout<<++m_Val<<"\tdemo(double)"<<endl; 
    } 

private: 
    int m_Val; 
}; 



int main() 
{ 
    SignatureDemo sd(5); 
    return 0; 
} 
+14

已包含頭? –

+1

如果你已經包含頭文件,你寫了'using namespace std;'? – guneykayim

+1

這與Qt或OOP無關,因此編輯。 –

回答

1

編譯器需要知道哪裏先找到std::cout。你只需要包含正確的頭文件:

#include <iostream> 

我建議你不要使用污染的指示using的命名空間。相反,無論是學習前綴性病類/對象與std::或使用特定using指令:

using std::cout; 
using std::endl;