Compiler..Microsoft visual C++?

Started by
25 comments, last by GamerTom 16 years, 6 months ago
Quote:Original post by Glak
Quote:Original post by kiome
Don't mislead beginners into bad habits, KrazeIke already offered a better solution.
Using 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.
Nope. 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;.

- Jason Astle-Adams

Advertisement
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?
________________James
Quote:Original post by Phantoms
So are we saying including
using namespace std;
is developing a bad habit for a beginner, or that it should be used?

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.
My Blog
So if you use - using std::cout; thats not a bad habit?
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:
#include <iostream>int main(){  using namespace std;  cout << "Hello World!\n";  f();}void f(){  cout << "This line will generate an error";}
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.

C++: A Dialog | C++0x Features: Part1 (lambdas, auto, static_assert) , Part 2 (rvalue references) , Part 3 (decltype) | Write Games | Fix Your Timestep!

thank you for the advice nobodynews

This topic is closed to new replies.

Advertisement