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;
}
cout << endl inserts a new line and flushes the stream(output buffer), whereas cout << “\n” just inserts a new line.
Use std::cout , since cout is defined within the std namespace. Alternatively, add a using std::cout; directive.
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.
Standard end line (endl) It is used to insert a new line characters and flushes the stream.
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;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With