-3

When i compile my c++ code using g++ 5.1.1 it says

"narc05b.cpp: In function ‘int main()’:

anarc05b.cpp:5:3: error: ‘cout’ was not declared in this scope
cout<<"hello\n"; ^ anarc05b.cpp:5:3: note: suggested alternative: In file included from anarc05b.cpp:1:0: /usr/include/c++/5.1.1/iostream:61:18: note: ‘std::cout’ extern ostream cout; /// Linked to standard output "

...what does it mean ?

Community
  • 1
  • 1
  • 1
    Isn't it the classic case of missing "using namespace std;"? – Arthur Gevorkyan Dec 10 '15 at 12:46
  • @ArthurGevorkyan: Your edit just made the compiler messages harder to read. Do you really think it was an improvement? – Blastfurnace Dec 10 '15 at 12:58
  • The compiler tells you: `cout` not found. Suggested alternative `std::cout`. – Bo Persson Dec 10 '15 at 13:15
  • @Blastfurnace, I do. Sorry. I hope it's not critical. – Arthur Gevorkyan Dec 10 '15 at 13:48
  • @ArthurGevorkyan - no, it's the classic case of not using a namespace prefix; the correct name is `std::cout`. `using namespace std;` is an abomination. – Pete Becker Dec 10 '15 at 14:23
  • @PeteBecker, yes. I just proposed the easiest solution for a beginner in C++. – Arthur Gevorkyan Dec 10 '15 at 14:33
  • @ArthurGevorkyan - teaching beginners things that they'll have to unlearn later is not the easiest solution. If someone applied to me for a job and their sample code was littered with 'using namespace std;` it would be a serious detriment to their chances of getting hired. – Pete Becker Dec 10 '15 at 14:36
  • @PeteBecker, whatever you say. However, I don't see any potential performance issues and/or name collisions caused in a project that utilizes only one library. For more details, there is Sam Varshavchik's comment below. I don't code in C++ for my living, thus don't want to start a discussion about the pros and cons of those 2 approaches. – Arthur Gevorkyan Dec 10 '15 at 14:42

1 Answers1

0

Use should use the namespace directive in the code

using namespace std;

or alternatively prefix cout with std::cout

Ajay
  • 775
  • 5
  • 19
  • http://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice – Sam Varshavchik Dec 10 '15 at 13:25
  • @SamVarshavchik was the question about the merit and demerits of using namespace directive ? I guess the Answer seeker is new to the coding in c++ env and the answer completely solves his query .... and yes I do agree using namespace has it demerits – Ajay Dec 10 '15 at 13:32
  • 1
    Yes, `using namespace std;` had its demerits. It's a bad habit to get into, and should **not** be recommended to beginners, since they'll just have to unlearn it later. – Pete Becker Dec 10 '15 at 14:25