Join 118,309 C++ Programmers for FREE! Ask your question and get quick answers from experts. There are 1,710 online right now! We've got more than 500 tutorials and 2,000 snippets. Join and find out why Dream.In.Code is the #1 programming help community on the internet! Registration is fast and FREE... Join Now!
No. You would make a .h file with declarations of functions/variables/etc.
You would then define them in a .cpp file.
Then, you would use #include "myHeader.h" to include it in your program.
You can just define things in a .h file, but it can cause errors later on. I tend to stick with a .h and a .cpp file, but if I can get away with it I will sometimes define it in the .h (on days when I feel lazy)
Edit: Make sure that all the .h and .cpp files are in the correct paths, I tend to store them all in the same place. However, you can access them from another folder, too. Say, for example, you have a character class, and want to put it in a seperate folder. Your project folder would then look something like: path/project-folder-name/character/character.h Now, since it is a path deeper than your actual project, you would include it like this: #include "character/character.h" But this is quite a rare and pointless habit, so it's kinda needless.
Another edit: If anyone's interested, post here with a request for a tutorial on setting up a project, including headers, libs, dlls, etc, and I'll gladly write one
No. You would make a .h file with declarations of functions/variables/etc.
You would then define them in a .cpp file.
Then, you would use #include "myHeader.h" to include it in your program.
You can just define things in a .h file, but it can cause errors later on. I tend to stick with a .h and a .cpp file, but if I can get away with it I will sometimes define it in the .h (on days when I feel lazy)
Edit: Make sure that all the .h and .cpp files are in the correct paths, I tend to store them all in the same place. However, you can access them from another folder, too. Say, for example, you have a character class, and want to put it in a seperate folder. Your project folder would then look something like: path/project-folder-name/character/character.h Now, since it is a path deeper than your actual project, you would include it like this: #include "character/character.h" But this is quite a rare and pointless habit, so it's kinda needless.
Another edit: If anyone's interested, post here with a request for a tutorial on setting up a project, including headers, libs, dlls, etc, and I'll gladly write one
Is it any tutorial or something that shows how to write a .h file? Or can you show me? It don't need to be a whole program! Just so i can see how it work
gabehabe, I'd be very interested in that tutorial if you don't mind writing it please? It would be very helpful & is sometihng I will need to know for this next year at uni.
Then, you would just type declarations of variables, functions, etc.
Basically, it's best to avoid any assignment of variables, functions in there. Take a look at this: This would be a header file:
cpp
double a,b,c; double add (double x, double y);
And this would be a .cpp file, used to define them:
cpp
#include "myHeader.h" // the name of the header file
a = 3; b = 2; c = 29929;
double add (double x, double y) { return x + y; }
BUT: We also use header guards on the header file, to prevent it from being included/declared more than once. The most common are: #pragma once but more commonly, this:
cpp
#ifndef MYHEADER_H #define MYHEADER_H // stuff goes in here #endif
So, let's revisit that header which we made earlier:
Then, you would just type declarations of variables, functions, etc.
Basically, it's best to avoid any assignment of variables, functions in there. Take a look at this: This would be a header file:
cpp
double a,b,c; double add (double x, double y);
And this would be a .cpp file, used to define them:
cpp
#include "myHeader.h" // the name of the header file
a = 3; b = 2; c = 29929;
double add (double x, double y) { return x + y; }
BUT: We also use header guards on the header file, to prevent it from being included/declared more than once. The most common are: #pragma once but more commonly, this:
cpp
#ifndef MYHEADER_H #define MYHEADER_H // stuff goes in here #endif
So, let's revisit that header which we made earlier:
Technically adding #include "somecode.cpp" to your code should work. All the #include directive does is add the contents of one file into the current file.
BUT!!! The proper way to do this is to create a header file (somecode.h) which contains all of the function declarations and public structures and classes etc. and then include THAT in your main file.
So lets say for example that I like to have a text centered in my program so I write a function "centerPrintf()"
CODE
int centerPrintf(const char *format, ...) { char buffer[81]; char myformat[256]; char *ptr = myformat; int len; //used to tell how much of the buffer was used. int spaces; va_list args; //the argument list va_start (args, format); //set up the argument list to be passed to vsprintf() //This is how we can pass along our argument list using // the vsprintf function. len = vsprintf (buffer, format, args); spaces = 40 - len/2; while (spaces--) { *ptr++ = 0x20; } spaces = 0; while (buffer[spaces]) { *ptr++ = buffer[spaces++]; } *ptr = 0x00; vprintf(myformat, args); va_end (args); return len; //number of characters that we needed }
So since I want to use this function in more than one program (or I just want to separate out different files to keep my program organized). So I make a header file for the function:
CODE
//Ensure that this header is only included once... #ifndef _CENTER_PRINTF #define _CENTER_PRINTF 1
int centerPrintf(const char *format, ...);
#endif
and I put the code into centerPrintf.c
CODE
#include <stdio.h> #include <stdarg.h>
int centerPrintf(const char *format, ...) { char buffer[81]; char myformat[256]; char *ptr = myformat; int len; //used to tell how much of the buffer was used. int spaces; va_list args; //the argument list va_start (args, format); //set up the argument list to be passed to vsprintf() //This is how we can pass along our argument list using // the vsprintf function. len = vsprintf (buffer, format, args); spaces = 40 - len/2; while (spaces--) { *ptr++ = 0x20; } spaces = 0; while (buffer[spaces]) { *ptr++ = buffer[spaces++]; } *ptr = 0x00; vprintf(myformat, args); va_end (args); return len; //number of characters that we needed }
Now I can create a project and add the centerPrintf.c file to it, and then have my main program include the header
Note that there is more that you can do, for example adding the centerPrintf function to a library and then putting the header file into the standard include directory and then all you need to do to include the file is add "#include <centerPrintf.h>" -- note you use the < > symbols when the header file is in the standard directory, and the quotation marks when it is just in the path.