GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 15-Jun-2004, 12:37
BobbyMurcerFan BobbyMurcerFan is offline
Member
 
Join Date: May 2004
Posts: 103
BobbyMurcerFan is on a distinguished road

Which Header Files to Use??


I'm using Visual Studio 6 and I am preplexed over which header files to use: stdio or stdio.h, iostream or iostream.h, cstdlib or stdlib.h, etc..

I've read that .h files should be avoided, but iostrem (no .h) does not support cout on my computer (while iostream.h does).

CPP / C++ / C Code:
#include <iostream>    //no .h

int main(void) {
	cout << "Hello." << endl;
	return 0;
}

// compile errors:
// 'cout' : undeclared identifier
// '<<' : illegal, right operand has type 'char [7]'
// 'endl' : undeclared identifier

I've also read that different header types shouldn't be mixed in the same code. So the following is not a good idea:

CPP / C++ / C Code:
#include <cstdio>    // no .h
#include <iostream.h>    // .h

A little direction on this would be much appreciated. THANKS!
  #2  
Old 15-Jun-2004, 12:48
eXiLe eXiLe is offline
New Member
 
Join Date: Jun 2004
Posts: 10
eXiLe is on a distinguished road
just add this line:
using namespace std;
after all the headers, don't put .h in the end of the file name and you won't have problems
  #3  
Old 15-Jun-2004, 18:35
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,245
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all
Quote:
Originally Posted by BobbyMurcerFan
I'm using Visual Studio 6 and I am preplexed over which header files to use: stdio or stdio.h, iostream or iostream.h, cstdlib or stdlib.h, etc..

I've read that .h files should be avoided, but iostrem (no .h) does not support cout on my computer (while iostream.h does).

C++ headers you should use the non-h version as mentioned.

If you are writing C, there are no non-h headers.
__________________

Age is unimportant -- except in cheese
  #4  
Old 15-Jun-2004, 21:21
sho sho is offline
Junior Member
 
Join Date: Jun 2004
Posts: 49
sho will become famous soon enough
can somebody explain why arent .h headers recommended in c++?? and what is the difference between using them or using its non .h version??

also what does the statement using namespace std; do??
  #5  
Old 16-Jun-2004, 01:42
aaroncohn's Avatar
aaroncohn aaroncohn is offline
Regular Member
 
Join Date: Feb 2004
Location: Bay Area, CA.
Posts: 564
aaroncohn is a jewel in the roughaaroncohn is a jewel in the roughaaroncohn is a jewel in the rough
Okay, so first you need to understand what namespaces are. A namespace is basically what the word says... it's a bunch of space with a name. The trick is that the space usually has some code in it. Now, I'm sure you know about scope and variable lifetime. If so, you know that when you enter a function, it is said to be in scope, meaning... you can see the contents of the function, but you can't see anything else. When you exit the function, it's said to be out of scope, and wherever you're taken is then in scope. So, you can no longer see what's in the function, and you can't access its variables. With namespaces, you're basically taking all the code in a defined space, and giving the space a name so that it can't be accessed unless it's in scope. This is where the using directive comes in. The using directive allows you to bring the contents of a namespace into scope so that you can use it. Here's an example.

CPP / C++ / C Code:
// Let this file be our header with data-types stored in it.

namespace myTypes
{
  enum RPS { ROCK, PAPER, SCISSORS };
  struct player
  {
    char *name;
    RPS choice;
  };
}
namespace myOtherTypes
{
  enum Weather { RAIN, SHINE, WIND, HAIL, SNOW };

  namespace nestedTypes
  {
    enum TimeZone { PST, EST, MNT };
  }
}

CPP / C++ / C Code:
// Let this be an implementation file #1
#include "thatheaderfile.h"

using namespace myTypes; // bring myTypes into scope

int main()
{
  player newGuy; // in scope because of the using namespace
  RPS    lame_variable; // also in scope because using namespace
  
  return 0;
}
Notice that after the using directive, I have the keyword namespace. This declares that everything within the specified namespace that has an identifer is in global scope and can be used anywhere in our program. But, what if we don't want to use everything in that header? Now I'm going to introduce you to the scope operator.

CPP / C++ / C Code:
// Let this be an implementation file #2
#include "thatheaderfile.h"

using myTypes::player; // brings only the "player" type into scope, nothing else

int main()
{
  player newGuy; // in scope because of scope operator
  RPS    lame_variable; // error: we don't see RPS; we only see "player".
  
  return 0;
}
Ahhh, new operator. Notice I didn't use the namespace keyword, because I want only the player item. The :: operator is also known as the scope operator. It can be used to access specific items, even if they're nested, and as you can see above, namespaces can be nested.

CPP / C++ / C Code:
// This isn't an implementation file... just some examples

using namespace myOtherTypes; // Brings everything in this namespace into scope, even namespaces nested within it(?)
using namespace myOtherTypes::nestedTypes; // Brings only nestedTypes into scope
using myOtherTypes::nestedTypes::TimeZone; // Brings only TimeZone type into scope
Any questions?
__________________
-Aaron
  #6  
Old 16-Jun-2004, 04:04
sho sho is offline
Junior Member
 
Join Date: Jun 2004
Posts: 49
sho will become famous soon enough
Thank you aaroncohn, what a good description, i didnt know about namespaces and the scope operator thanks for explaining them.

So i would guess using namespace std; would mean to put in global scope all the variables and functions in the std namespace of one of the header files wouldnt it??

My question now is.. which header files?? and what are the differences between .h headers and non .h headers?? and why arent recommended the first ones???
  #7  
Old 16-Jun-2004, 04:54
aaroncohn's Avatar
aaroncohn aaroncohn is offline
Regular Member
 
Join Date: Feb 2004
Location: Bay Area, CA.
Posts: 564
aaroncohn is a jewel in the roughaaroncohn is a jewel in the roughaaroncohn is a jewel in the rough
Many standard C++ header files use the std namespace. That way you don't have to remember 50 different namespace identifiers! We don't use the .h extension for C++. C does not support namespaces, and it also does not support leaving out the .h extension. I guess the missing .h extension dignifies a file as C++ compliant and therefore supporting namespaces. Someone please correct me if I'm wrong. Occasionally, though, you will run into a header that has no C++ counterpart and will have to make the unsavory mixing of the .h and non-.h headers. I did this in my recent hangman game. There's no <time>, so I had to use <time.h> even though the program is almost strictly C++.
__________________
-Aaron
  #8  
Old 16-Jun-2004, 18:15
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,245
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all
Quote:
Originally Posted by aaroncohn
C does not support namespaces, and it also does not support leaving out the .h extension. I guess the missing .h extension dignifies a file as C++ compliant and therefore supporting namespaces. Someone please correct me if I'm wrong.
OK, you asked for it
Actually, C supports any file extension as a header, but only .h files come with a C compiler. You can still create your own header with or without an extension.

For C++ headers, the old-style .h headers also don't support namespaces, but the extensionless headers do -- which is the current standard. I don't have a clue how they work, but that's what I've noticed.

Quote:
Originally Posted by aaroncohn
Occasionally, though, you will run into a header that has no C++ counterpart and will have to make the unsavory mixing of the .h and non-.h headers. I did this in my recent hangman game. There's no <time>, so I had to use <time.h> even though the program is almost strictly C++.
Are you sure there wasn't a ctime header? Many of the old C headers (stdio.h, stdlib.h have C++ counterparts, usually named cstdio, cstdlib. VC 6 does.
__________________

Age is unimportant -- except in cheese
  #9  
Old 16-Jun-2004, 18:29
sho sho is offline
Junior Member
 
Join Date: Jun 2004
Posts: 49
sho will become famous soon enough
So the main difference between .h headers and non .h headers are that the first ones doesnt support namespaces isnt it?

Then what would happen if we include the non .h headers without using namespace std;?? we can also use their functions cant we?? if so the what is the help of that statement?

And if so, then if we include and use those headers without using namespace std; is the same as using the .h headers isnt it?? then i wouldnt find a great difference between the use of both...
 
 

Recent GIDBlogDeveloping GUIs with wxPython (Part 2) by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
this is from 6 files now i need fancy reports to write to the files kilgortrout C++ Forum 1 21-May-2004 16:57
Can't view pages from another machine on the Intranet aevans Apache Web Server Forum 9 14-May-2004 02:26
uploading files into server prinzekay MySQL / PHP Forum 5 16-Mar-2004 00:00
gxx linker accepts only 7 object files danielxs66 C++ Forum 1 12-Dec-2003 09:27
publishing Drirector/Shockwave files ...? darrin Web Design Forum 1 04-Oct-2002 17:21

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 20:25.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.