Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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;
}
like image 641
user2873416 Avatar asked Oct 12 '13 07:10

user2873416


People also ask

What is cout and Endl?

cout << endl inserts a new line and flushes the stream(output buffer), whereas cout << “\n” just inserts a new line.

How do I fix error cout was not declared in this scope?

Use std::cout , since cout is defined within the std namespace. Alternatively, add a using std::cout; directive.

Do you need Endl after cout?

In this case you don't need to tell the computer "this is the end of the line" because you say with the ";" it's the end of the line of code. The "endl" streamoperator is only necessary, if you want to show something to the user in the console and break a line in the output.

What is << endl?

Standard end line (endl) It is used to insert a new line characters and flushes the stream.


1 Answers

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;
like image 148
Shoe Avatar answered Sep 28 '22 07:09

Shoe