#include <"iDontGetThis.h">

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Apr 2008
Posts: 177
Reputation: Black Magic is an unknown quantity at this point 
Solved Threads: 4
Black Magic's Avatar
Black Magic Black Magic is offline Offline
Junior Poster

#include <"iDontGetThis.h">

 
0
  #1
Jul 9th, 2008
Hey,

I have seen some code examples were people include there own files, e.g

  1. #include <myFile.h>

do i just have to save a file called myFile.cpp?

and if so can i declare a function in myFile and then use it in untitled1 without declaring it just including myFile. Thanks.
C Plus Plus Coder.
Fourteen Years Of Age
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 401
Reputation: CoolGamer48 is on a distinguished road 
Solved Threads: 40
CoolGamer48's Avatar
CoolGamer48 CoolGamer48 is offline Offline
Posting Pro in Training

Re: #include <"iDontGetThis.h">

 
1
  #2
Jul 9th, 2008
You would actually name the file myFile.h, not myFile.cpp. When you #include something you are basically taking the contents of the header (*.h) file and dumping them into your current .cpp file. If the file you need to include is in your current working directory (or if you're specifying an absoltue path), use "". If the file is in one of your compiler's include directories, use <>.

It is a bad idea to define functions in a header file. If you have this in myFile.h:
  1. int MyFunc()
  2. {
  3. return 5;
  4. }

and you have two .cpp files in your project, and they both #include myFile.h, you'll get an error, because you're trying to re-define a function, a no-no. Instead, have prototypes in your header files. Like:
myfile.h
  1. int MyFunc();

and have definitions in a .cpp file:
myfile.cpp
  1. #include "myfile.h"
  2.  
  3. int MyFunc()
  4. {
  5. return 5;
  6. }

Then any amount of other files can #include myfile.h with no problem, as long as myfile.cpp is also in the project.

One last thing, it's good to put the code in your header files in #ifndef blocks. Like this:
  1. #ifndef MY_FILE_H
  2. #define MY_FILE_H
  3.  
  4. int MyFunc();
  5.  
  6. #endif

This way, if you end up #including the same file twice in one .cpp file (which can happen when you have lots of #includes), you won't have an issue, since the code is only executed once.
Last edited by CoolGamer48; Jul 9th, 2008 at 2:37 pm.
I'm a student. If my statements seem too absolute, feel free to coat them with "In my opinion..." or "I believe...".
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC