I tried to run hello world in C++ on sublime text 2 on my mac.
I typed
#include "iostream"
int main()
{
cout<<"Hello WOrld";
return (0);
}
but it gives me an error
/Users/chiragchaplot/q2.cpp:5:2: error: use of undeclared identifier 'cout'; did you mean 'std::cout'?
cout<<"Hello World";
^~~~
std::cout
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/c++/v1/iostream:50:33: note: 'std::cout' declared here
extern _LIBCPP_FUNC_VIS ostream cout;
^
1 error generated.
[Finished in 0.4s with exit code 1]
The following methods will solve your problem:
Method 1 : (BAD PRACTICE)
Adding the following line before main function.
So your code will now become:
Method 2 : (GOOD PRACTICE)
You can simply write
std::cout
instead ofcout
.Full code with
std::cout
This tells the compiler that the
cout
identifier comes from thestd
namespace.Method 2 is better than Method 1. Further reading : Why is "using namespace std" considered bad practice?
For more information on namespaces, check out the following links: