Quote:Original post by GlakNope. The std namespace contains standard library functions and is something people definitely should be using if they're writing C++. The bad habit being mentioned is beginning the program with using namespace std;.Quote:Original post by kiomeUsing the names in the std namespace is the bad habit that needs to be avoided. The std namespace exists to allow backwards compatability for legacy code; not to be a proper namespace. I think that not using namespace std is the bad habit.
Don't mislead beginners into bad habits, KrazeIke already offered a better solution.
Compiler..Microsoft visual C++?
Quote:Original post by GamerTom
Thanks again KrazeIke, it works now, can oyu or someone exaplain and help me understand what int is and why did it need it in the code, cause when I wrote it on other compilers it worked without the int.
"int" is the type of the return value that the "main" function usually gives back to the OS. Other compilers might accept a missing return value because they operate in C-compatibility mode. In C, every function is assumed to return an "int" value by default. This is not the case with C++, so a more strict (e.g. compliant) compiler will not accept a missing return type for a function.
This is one of the few incompatibilities between C and C++.
So are we saying including
using namespace std;
is developing a bad habit for a beginner, or that it should be used?
Quote:Original post by Phantoms
So are we saying includingis developing a bad habit for a beginner, or that it should be used?using namespace std;
Don't use "using namespace std;".
If you really want less typing when using common things like cout, one could declare "using std::cout;". That way you can call "cout" without the namespace and it wouldn't open the whole std namespace to your application.
It's not a good habit, that's for sure. Actually, you should consider doing something like this if you Really want to save typing:Putting the using statement inside a code block is perfectly legal. This way you don't pollute the global namespace. And if a namespace collision were to happen you only need to modify the functions that do this.
#include <iostream>int main(){ using namespace std; cout << "Hello World!\n"; f();}void f(){ cout << "This line will generate an error";}
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement