简体   繁体   中英

Cout and endl errors

I have listed my code below. I get soooo many errors saying cout and endl was not declared in this scope. I do not know what I am doing wrong or how to force the class to recognise cout? I hope I am explaining my problem correctly. If I comment out the methods (not the constructor) it works. I am probably just making a novice mistake here - please help.

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

The compiler needs to know where to find std::cout first. You just need to include the correct header file:

#include <iostream>

I'd suggest you not to pollute the namespace using using directives. Instead either learn to prefix std classes/objects with std:: or use specific using directives:

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

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