Author Topic: What happened to C++? It evolved without me!  (Read 5140 times)

halkun

  • Global moderator
  • *
  • Posts: 2097
  • NicoNico :)
    • View Profile
    • Q-Gears Homepage
What happened to C++? It evolved without me!
« on: 2005-01-12 02:56:37 »
Ok, So I have decided to attempt OO programming *again* with C++. I doug through my bookshelf and found my "Teach Yourself C++ in 21 Days" book, and took to writing my first "Hello World" prorgam.

Code: [Select]

#include <iostream>
int main()
{
    cout << "Hello World!\n";
    return 0;
}


g++ barfed on this at compile time.

Guess what, iostream.h seems to be deprecated! That lib is, like, in every single C++ tutorial I've ever seen! It would seem that I have to make some changes to *EVERY* example in my book, and get around things one of two ways.

First, I have to change <iostream.h> to <iostream>.

then I have type
Code: [Select]

using namespace std;

after my modified #include <> statement. This "kinda fixes" the cout command, but I can't seem to get this to work.
Code: [Select]

cout "\n";

Which is an example in the book to make a new line.

Other than that I can drop the "namespace" thingy and then have to tell the compiler to use cout like this..

Code: [Select]

std::cout << "Whatever.\n";


Now it seems that when you use the "std::" method, I have to prefix not only "cout" but other functions like "endl" with this "std::" thing. (Whatever that means, I'm guessing it has something to do with 'namespace' and telling it to use "std", but as I don't know what namespace is either, or what 'std' referrs to, or why I need to seperate these two commands with a double-colon, so that doesn't help me in the least.)

This isn't going so well for my first day. I can compile 20 year old C programs in gcc, but I can't compile a 6 year old C++ program?

I'm continuing anyway, but I'm going to have to rewrite code when I come across it, not knowing what it does. Is there any other issues I'm going to have to worry about. It seems that basic I/O commands have changed an I'm probably going to be banging my head aginst the wall then it comes to input or even *gasp* accessing a file!

Little help here?

Aaron

  • *
  • Posts: 2818
    • View Profile
    • http://aaron-kelley.net/
What happened to C++? It evolved without me!
« Reply #1 on: 2005-01-12 03:23:52 »
The <iostream> instead of <iostream.h> is a new standard.  Since I first started learning C++ at my university about a year and a half ago, that's what they've been teaching, but I think Microsoft Visual Studio still supports both.  (I don't know if it would just be a matter of having all the old .h files present in the include directory where your compiler looks for them?)

As I understand it, the "new standard" was created in an effort to reduce "feature duplication" between the standard .h files... for instance, iostream.h and fstream.h provided some of the same functionality.  (I don't know if this is the real reason, but one of my professors said something about this.)

You have to use the namespace for your program to know where to look for stuff, same goes for when you include anything like <string>, <fstream>, <iomanip>, <cmath>, and so on and try to use them, or you'll get an error...

Even if I don't really know why this is, I can write code that works.

Quote
Code: [Select]
cout "\n";

Shouldn't it be...
Code: [Select]
cout << "\n";
Right?
That first program you wrote up in your post should be fine if you added using namespace std; after the include statement.

I don't know if accessing a file is any different than it was before, I never tried with the "old standard," but these days you can do something like...

Code: [Select]
#include <iostream>
#include <fstream>
using namespace std;

int main()
{
    ifstream file;
    file.open("filename.ext");
    if(!file) { cout << "File open error.\n\n"; exit(1); }
    // ...
}


Then you can read stuff out of the file with file >>, like getting stuff from cin.
You can use an fstream object instead and declare what you want to be able to do with the file when you open it (open it in binary mode for instance).

Anyway you can probably figure this all out...  Anyway, if you can get into OOP, it's great, I've done things that I couldn't imagine doing with a more procedural language.  But it took a year and a half of C++ in college to get there, and now I think I can consider myself a little bit of a competent programmer.

Qhimm

  • Founder
  • *
  • Posts: 1996
    • View Profile
    • Qhimm.com
What happened to C++? It evolved without me!
« Reply #2 on: 2005-01-12 03:35:25 »
The cout "\n"; example is erroneous and should indeed be cout << "\n"; -- there is no syntax in C++ for statements like that without operators (i.e. "blank space" does not carry any semantic meaning in C++).

As for the std namespace, all the C++ standard libraries reside in a separate namespace to help make a clear division between the library and application code. While you can insert a using namespace statement to clear up your code a bit, the advantage is still that all functions reside in a separate "area" less likely to conflict with whatever code you write yourself. For example, if you write your own function named just like some stdlib function, they don't conflict, and your own function takes precedence in your code while the stdlib function takes precedence in stdlib code; just like it's supposed to.

NOTE: By writing 'using namespace std;' you in effect tell the compiler to also look in the std namespace for any function names found in your code. Imagine a namespace as a directory and 'using namespace' as an added include path.

Anyway, sadly the most problems you'll encounter at this stage are caused by sloppy example writers. It's a bitch but you'll get through it soon enough. As for the "<<" stream syntax, well... I don't personally like it. It's a nice concept for abstraction, but it suddenly makes it difficult to exercise more precise control over the stream. Fortunately, as is common in C++, you are not required to adhere to any new feature, it's just one more way of doing things.

sfx1999

  • *
  • Posts: 1142
    • View Profile
What happened to C++? It evolved without me!
« Reply #3 on: 2005-01-12 03:40:20 »
What is so new? You could always use <iostream> instead of <iostream.h>. The thing is, it would be in a namespace instead, so if you didn't put in, "using namespace std;", you would use std::cout instead.

Sometimes, it is good to type it out, because the vector class can have problems if you are using an actual class called vector for vectors.

I think the big problem is when you find out about new standars. In GCC, I have seen errors like, "Scope changed for new ANSI for scoping (I don't know exactly what the message was, but it means you have to pull the declaration of a variable out of your for loop) and function main must return type int.

Aaron

  • *
  • Posts: 2818
    • View Profile
    • http://aaron-kelley.net/
What happened to C++? It evolved without me!
« Reply #4 on: 2005-01-12 03:45:14 »
Quote
What is so new? You could always use <iostream> instead of <iostream.h>. The thing is, it would be in a namespace instead, so if you didn't put in, "using namespace std;", you would use std::cout instead.

My first programming class experience was in my senior year of high school (2003), and for some reason, the little school that was offering the class was using a C++ compiler for DOS that was made in 1994.  I just checked, and it only supports <iostream.h>, so I figure this new standard is newer than that. :P

(Anyway, I used Visual Studio 6 for that class, all the code still worked fine.)

halkun

  • Global moderator
  • *
  • Posts: 2097
  • NicoNico :)
    • View Profile
    • Q-Gears Homepage
What happened to C++? It evolved without me!
« Reply #5 on: 2005-01-12 04:05:41 »
g++ allows me to use <iostream.h> but if I don't add a -Wno-deprecated switch on the command line , I get a warning that's about 400 pages long spewed at me on how .h files suck. It goes on to say I should really be using namespace, then comes examples on using std:: and then a happy little note that says to use -Wno-deprecated if I didn't want this warning to come up.

Here is the actual warning, and then I have to get to homework....

Quote

halkun@dokkun:~/dokkun/projects/cpplab/chap01> g++ hello.cpp -o hello
In file included from /usr/include/g++/backward/iostream.h:31,
                 from hello.cpp:1:

/usr/include/g++/backward/backward_warning.h:32:2: warning: #warning This file includes at least one deprecated or antiquated header. Please consider using one of the 32 headers found in section 17.4.1.2 of the C++ standard. Examples include substituting the <X> header for the <X.h> header for C++ includes, or <sstream> instead of the deprecated header <strstream.h>. To disable this warning use -Wno-deprecated.

halkun@dokkun:~/dokkun/projects/cpplab/chap01>


You would think this would be something spit out on a -pedantic switch!

mirex

  • *
  • Posts: 1645
    • View Profile
    • http://mirex.mypage.sk
What happened to C++? It evolved without me!
« Reply #6 on: 2005-01-12 07:02:40 »
Aye, why not to use <stdio.h> ? Its easy to use, and without any problems. I hope. It works for me everywhere (well works in dos BC and windows MSVC). Halkun try it, its easier :-P

Code: [Select]

#include <stdio.h>

printf( "Hello world.\n" );
printf( "\n" );

int  i = 7;
char str[] = "string";  //dunno if this will be supported by your compiler
printf( "Printing number and string: %i, %s\n", i, str );


 and using files is quite easy too !
Code: [Select]
#include <stdio.h>

FILE  *fin, *fout;

fin = fopen( "your_input", "rb" );  // reading binary file
if ( fin == NULL ) {
  printf( "Aye input file does not exist!" );
  return;
}
fout = fopen( "your_output", "wt" ); // writing text file
if ( fout == NULL ) {
  printf( "Arrr output file could not be created!" );
  return;
}

struct {
  unsigned long ul; // 4 bytes
  unsigned char uc; // 1 byte
} data;

// read data from file to the data structure
fread( &data, 1, sizeof( data ), fin );

// print output to the screen
printf( "Data in the file were: %i, %i\n",
  (int)data.ul, (int) data.uc );

// print output to the file
fprintf( fout, "Data in the file were: %i, %i\n",
  (int)data.ul, (int) data.uc );

fclose( fout );
fclose( fin );