Cout 和 endl 错误

use*_*416 6 c++ io compiler-errors

我在下面列出了我的代码。我收到很多错误,说 cout 和 endl 未在此范围内声明。我不知道我做错了什么或如何强制班级识别 cout?我希望我能正确解释我的问题。如果我注释掉方法(不是构造函数),它就可以工作。我可能只是在这里犯了一个新手错误 - 请帮忙。

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;
}
Run Code Online (Sandbox Code Playgroud)

Sho*_*hoe 8

编译器首先需要知道在哪里找到std::cout。您只需要包含正确的头文件:

#include <iostream>
Run Code Online (Sandbox Code Playgroud)

我建议您不要使用using指令污染命名空间。相反,要么学习std::使用特定using指令作为std 类/对象的前缀:

using std::cout;
using std::endl;
Run Code Online (Sandbox Code Playgroud)