Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 [C++] Things you shouldn't be doing (and what you should)
Index -> Programming, C++ -> C++ Tutorials
View previous topic Printable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
wtd




PostPosted: Mon Nov 08, 2004 8:56 pm   Post subject: [C++] Things you shouldn't be doing (and what you should)

[list]
[*]Don't include "iostream.h".

This header, along with many others, have been deprecated by the C++ standard. This means that they may work now, but aren't guaranteed to work in future compilers that stick to the standard.

Instead...

code:
#include <iostream>


Aside from "stdlib.h", you probably won't encounter any standard header files that have the ".h". You will find this on nonstandard header files, though, including your own, so beware, and when in doubt, ask.
[*]Don't use "cout" or "cin" without "using namespace std".

There are three ways to properly do this:

  • code:
    using namespace std;

  • code:
    std::cout

  • code:
    using std::cout



More to come.
Sponsor
Sponsor
Sponsor
sponsor
Geminias




PostPosted: Wed Sep 21, 2005 11:28 pm   Post subject: (No subject)

please explain why we should use std, what does std mean and what does namespace mean? i'm not saying your wrong just want to know why.
wtd




PostPosted: Wed Sep 21, 2005 11:46 pm   Post subject: (No subject)

In a C++ program, you can't give two things different names. It just doesn't work.

So, if you have two different libraries that both want to have the name "copy", you'd have to give them reallyLongAwkwardNames to keepThemFromConflictingWithEachOther. Wink

Or.... you can use namespaces. With a namespace, instead of, for instance: the WoobleSoftCopyRoutine and NinjaStudiosCopyRoutine functions, you might have:

WoobleSoft::CopyRoutine and NinjaStudios::CopyRoutine.

Now, if I'm not using both at the same time, for convenience, I can do something like:

code:
#include <wooblesoft.h>

int main()
{
   using namespace WoobleSoft;

   CopyRoutine();

   return 0;
}


Now, what is "std"? No, it's not something you catch from doing the reproductive equivalent of using Windows without a firewall.

It's an abbreviation for "standard". It's a namespace that holds all kinds of useful standard library functions, classes and variables.
Geminias




PostPosted: Thu Sep 22, 2005 7:55 pm   Post subject: (No subject)

hey wtd.. i'm using mingw compiler it doesn't compile if i use <iostream>

example (this one does compile right)

Quote:
#include <iostream.h>

int main ()
{
cout << "Hello World! "; // says Hello World!
cout << "I'm a C++ program"; // says I'm a C++ program
return 0;
}


and this one does not

Quote:
#include <iostream>

int main ()
{
cout << "Hello World! "; // says Hello World!
cout << "I'm a C++ program"; // says I'm a C++ program
return 0;
}


the trully gay thing i find is that it will give error and tell me that cout isn't defined with <iostream> and it won't run period. and when i use <iostream.h> it runs but gives me all these warnings about using a deprecated header and i should replace it with <iostream>

sorry for my noob questions

and the reason i didn't use std::cout there is because it seems to just add more confusion to the thing.. like i'm wondering if i have to declare something other than <iostream> if i'm using std
[Gandalf]




PostPosted: Thu Sep 22, 2005 7:59 pm   Post subject: (No subject)

You have to either use std:: or else have something like:
code:
using namespace std;

near the beginning of your code. You need to tell the compiler that it is getting the cout from the standard namespace.
Geminias




PostPosted: Thu Sep 22, 2005 10:50 pm   Post subject: (No subject)

but how do you explain my mingw compiler not compiling my # include <iostream> but it does compile the .h iostream. Also i keep getting a "warning": no newline at end of file. what is this all about?

so far i have to say that using dev-c++ made the whole process more intuitive and therefore easier for me to grasp. and it ran both iostreams without giving me bull shit.. and it ran the std::cout's np too...
wtd




PostPosted: Thu Sep 22, 2005 11:23 pm   Post subject: (No subject)

Geminias wrote:
but how do you explain my mingw compiler not compiling my # include <iostream> but it does compile the .h iostream. Also i keep getting a "warning": no newline at end of file. what is this all about?

so far i have to say that using dev-c++ made the whole process more intuitive and therefore easier for me to grasp. and it ran both iostreams without giving me bull shit.. and it ran the std::cout's np too...


G++, via MinGW works fine.

Could you please post your code in its entirety, so we can ty to help you figure out what's going on?

Dev-C++ may seem like a good thing, but it teaches deprecated C++: old features that aren't helpful to you as a student.
Geminias




PostPosted: Fri Sep 23, 2005 3:07 pm   Post subject: (No subject)

yeah i'm taking your word for it and sticking with mingw.

i have to learn java in school so when we start that it will be helpful to be able to write the prog in textpad and use the command line to compile it rather than have to get oriented with a whole new gui.
Sponsor
Sponsor
Sponsor
sponsor
[Gandalf]




PostPosted: Fri Sep 23, 2005 3:13 pm   Post subject: (No subject)

See, that's where I find the slight problem. MinGW comes with a java compiler too, but it is different from the Sun compiler. Most people prefer/use the Sun compiler because it is the 'official' one, so it seems like a waste of space. Still, it's good to have the option.
rizzix




PostPosted: Fri Sep 23, 2005 6:15 pm   Post subject: (No subject)

gcj uses gnuclasspath which is an incomplete implementation of the J2SE specs... (not to mention it's not just as optimised as SUN's jvm) so please refrain from using it.. otherwise it's quite ok.
Display posts from previous:   
   Index -> Programming, C++ -> C++ Tutorials
View previous topic Tell A FriendPrintable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 10 Posts ]
Jump to:   


Style:  
Search: