+ 3

Where is the problem ?

In class exactly cpp file Error:"cout " was not declared in this scope knowing that i used using namespace std

19th Jun 2020, 3:26 PM
Taha Houari
Taha Houari - avatar
10 odpowiedzi
+ 3
On top of the beginning of your code add this- #include <iostream> Recommendation:- Its better to use . std::cout<< Instead of using namespace std; You just need to add "std::" in front if cout. Note* Typing #include <iostream> is compulsory
19th Jun 2020, 8:54 PM
Shankhui Lunghar
Shankhui Lunghar - avatar
+ 2
std::cout << "Whatever you were trying to print" << endl;
19th Jun 2020, 3:31 PM
BroFar
BroFar - avatar
+ 2
Put the following code before int main(): using namespace std; And you will be able to use cout. For example: #include<iostream> using namespace std; int main(){ char t = 'f'; char *t1; char **t2; cout<<t; return 0; } Now take a moment and read up on what cout is and what is going on here: http://www.cplusplus.com/reference/iostream/cout/ Further, while its quick to do and it works, this is not exactly a good advice to simply add using namespace std; at the top of your code. For detailed correct approach, please read the answers to this related SO question.
21st Jun 2020, 11:10 AM
Kartik Mishra
Kartik Mishra - avatar
+ 2
Shankhui Lunghar Of course it's not required, but his question implied that he wanted to write is as cout << (EXPRESSION); rather than substitute every time "cout" appears in his code for "std::cout". Regarding your statement that "it will cause global confusion", what do you mean by "global"?
25th Jun 2020, 8:53 PM
SapphireBlue
SapphireBlue - avatar
+ 1
Put this at the top of your file: #include <iostream> using std::cout; (Please don't do "using namespace std;"; even though many programmers do it, it's considered to be bad practice, because if you have a variable, function, or type with the same name as a member of the namespace "std", your code won't compile)
21st Jun 2020, 2:55 AM
SapphireBlue
SapphireBlue - avatar
0
Sapphire i am confident that "using" is not required,its just std::cout<<..........; And also it will cause global collision.
21st Jun 2020, 7:35 AM
Shankhui Lunghar
Shankhui Lunghar - avatar
0
Add "std::" before cout<<
21st Jun 2020, 9:47 AM
Ânkita Bibave
Ânkita Bibave - avatar
- 2
#include "employee.h" using namespace std; void employee::setname(string n) { if(n.length()<3) { cout<<"name is too short\n"; } else{ name=n; }} void employee::setage(int a) { age=a; } void employee::setsalary(double s) { salary=s; } string employee::getname() { if (name==" ") { return "not defined"; } else { return name; }} int employee::getage() {return age;} double employee::getsalary() { return salary; } employee::employee() { //ctor } employee::~employee() { //dtor }
19th Jun 2020, 3:32 PM
Taha Houari
Taha Houari - avatar