Click to See Complete Forum and Search --> : migrating


Fingel
02-10-2003, 12:08 AM
Hi,

I'm used to programming in M$ visual C++ (NOT BASIC, never did like it) and I'm wondering what the easiest way would be to make the transistion to programming for linux.
Just to try, I typed out this in Kate:

#include <iostream.h>

int main()
{
cout <<"hello";

return 0;

}

I attemted to run it in the konsole, but I just get syntax errors. what am I doing wrong? Is there somthing different? I also tried to write this in Kdevelop, still, it dosnt work. Any help would be appreciated.

tecknophreak
02-10-2003, 12:42 AM
1. Create code and save as hello.cpp

#include <iostream>

using namespace std;

int main()
{
cout <<"hello" << endl;

return 0;

}

2. Compile the code from a terminal:

g++ hello.cpp -o hello

3. Run the program from a terminal:

./hello

Fingel
02-10-2003, 02:57 AM
whats this "using namespace std" ?

bwkaz
02-10-2003, 10:17 AM
Originally posted by Fingel
whats this "using namespace std" ? It's evil, see my sig. ;)

The ISO C++ draft standard replaced <*.h> headers (for C++ anyway, not for C) with headers that don't have .h on the end of them (you now should #include <iostream>, #include <sstream>, etc.). The difference between the two is that the *.h headers put every class and every symbol (cout, ifstream, etc., etc.) into the default global namespace, which cluttered it with a LOT of extraneous "stuff".

The new <iostream> etc. header files declare everything in a std namespace. So if you don't have any using statements anywhere, you have to use std::cout, std::endl, std::cin, etc., etc. for everything from the header. Now a using namespace std; will import the ENTIRE std namespace into the default global namespace. But that defeats the purpose of even having a "std" namespace -- the purpose was to reduce clutter, remember? You've just cluttered up the global namespace again with that.

What I do is only import the symbols I need. Something like:

using std::cout;
using std::ifstream;
using std::endl;
using std::ios; so that in my code I can do cout << "whatever" << endl;, and ifstream in("filename", ios::in); It's clean, since you only import the symbols you need.

Fingel
02-10-2003, 01:43 PM
thanks. that helps