6

我在下面列出了我的代码。我收到很多错误,说 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;
}
4

1 回答 1

9

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

#include <iostream>

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

using std::cout;
using std::endl;
于 2013-10-12T14:01:59.793 回答