| | |
#include <"iDontGetThis.h">
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
Hey,
I have seen some code examples were people include there own files, e.g
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.
I have seen some code examples were people include there own files, e.g
C++ Syntax (Toggle Plain Text)
#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
Fourteen Years Of Age
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:
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
and have definitions in a .cpp file:
myfile.cpp
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:
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.
It is a bad idea to define functions in a header file. If you have this in myFile.h:
C++ Syntax (Toggle Plain Text)
int MyFunc() { return 5; }
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
C++ Syntax (Toggle Plain Text)
int MyFunc();
and have definitions in a .cpp file:
myfile.cpp
C++ Syntax (Toggle Plain Text)
#include "myfile.h" int MyFunc() { return 5; }
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:
C++ Syntax (Toggle Plain Text)
#ifndef MY_FILE_H #define MY_FILE_H int MyFunc(); #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...".
![]() |
Other Threads in the C++ Forum
- Previous Thread: Form1 and Form2
- Next Thread: String/Class problem(s)!!
| Thread Tools | Search this Thread |
api application array arrays based beginner binary c++ c/c++ calculator char char* class classes code compile compiler console conversion count delete deploy desktop directshow dll download dynamic dynamiccharacterarray encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg simple sorting string strings temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets





