Using other .cpp files and or h files

My knowledge of C++ is limited. I understand this; the main CPP file is where I store my code. Problem is, my current CPP is becoming huge, way too many functions. When I research header files and other cpp files on this site and others, it seems they use other commands for them like "void main()" or things other than "main". These typically use other weird commands and variables etc that I just don't get...yet. So I'm hoping someone can at least give me a SIMPLE example. If one does not exist, then I understand that I'd then have to research a buttload of stuff to understand it.

*Note: I am NOT asking someone to go through all the "void" and other commands in detail, hoping there's an easier way*

My question is this; how do I just make the simplest file so that any function I call is the same as if it were stored in my main file? Where little (or preferably NO modification to any variable, none of the "public" or "private" stuff is of concern nor needs to be declared).

IE:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
int addsometothis()
{
     a = 5;
     b = 7;

     z = dothis[a][b];
     dothis[y][b] = z;

     return 0;
}

int main()
{
     addsometothis();
 
     cout << "End of Program" << endl;

     return 0;

}


That is an example of my program. It isn't the program, but just meant as a basic example. If you were to cut and paste my "addsometothis()" function to a file, told me what file it was called, how to include it into my main cpp file, that would answer my question.

or do all I need to do is add a .cpp file to my project and put this into it:
1
2
3
4
5
6
7
8
9
10
int addsometothis()
{
     a = 5;
     b = 7;

     z = dothis[a][b];
     dothis[y][b] = z;

     return 0;
}


And then I would be able to call it from any other function throughout my program? Or would it have to be a header file? I just want to break it up into other files so it is more organized and I no longer have over 100 functions staring at me across the void of my desk whenever I'm going through it.
1) Make a header file: Header.hpp (or whatever name you like), and put the declarations of the functions you would like to use in it.
2) Then make a source file: Source.cpp (again, could be any name you like), put the function definitions (the bodies) in there.
3) Then include Header.hpp in your main file, then you can use the functions you declared in Header.hpp there.

Example:

Header.hpp
1
2
3
4
5
6
7
#ifndef HEADER_HPP
#define HEADER_HPP

// Declarations
int addsometothis();

#endif 


Source.cpp
1
2
3
4
5
6
7
8
9
10
11
12
#include "Header.hpp"

int addsometothis()
{
     a = 5;
     b = 7;

     z = dothis[a][b];
     dothis[y][b] = z;

     return 0;
}


main.cpp
1
2
3
4
5
6
7
8
9
#include "Header.hpp"

int main()
{
    // Here you can use it
    addsometothis();

    return 0;
}


Btw, this also holds true for classes: class declaration in the header, definition in the source.
Last edited on
Thank you, it seems you've made it incredibly simple for me. A few questions remain.

1. Do I simply include the "source.cpp" in the project and that's it, the program/compiler will know to add it?

2. Will this treat all variables and such I declare, or drag values from and put into the function in source.cpp, as if the function was written in the main.cpp?

3. This one may be a little more complicated and may be one you may say I should research it instead, I totally understand, but I figure it doesn't hurt to ask.

What is the significance, in general, of "ifndef HEADER_HPP"? define HEADER_HPP? #endif? An .HPP file?

4. Do I have to have the "Header.hpp" included in both the main.cpp and source cpp?

Thank you again.
1. Yes, most times yes.
2. The variable must be EXTERN variables like this:

1
2
3
4
5
/* .h file */
extern int Test;

/* .cpp file */
int Test = 0;


Then it will be accessible like it was COMPLETELY global.

3. It means that, if you include more times the same file, it won't get included again, avoiding some compiler errors.
Reason? Think of it like this:

1
2
3
4
5
6
7
8
9
/* random .h file */
struct Test {
};

/* main.cpp */
#include "random.h"
#include "random.h" // include it twice.
// Error happened! "struct Test" is already defined!
// #ifndef/#define/#endif avoid these errors 


4. Yes, if you need to access the functions in both source files.

Example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
/* a.h file */

void test();

/* a.cpp file */
#include "a.h"
void test(){}

/* b.h file */
void test2();

/* b.cpp file */
#include "b.h"
#include "a.h"
void test2(){test();}

/* main.cpp file */
// Now, for this example, main.cpp does NOT directly need a.h.
// But it needs b.h.
#include "b.h"

int main()
{
    test2();
// will call test(), even if you didn't include a.h and b.h doesn't include a.h
}


Most times you can include all files without needing to really understand if you need it or not.
Last edited on
Thank you. To further confirm. Each variable I have in the main.cpp file that I wish to use globally, in both the main.cpp and the source.hpp file, I have to re-declare it with an "extern" before it in the .hpp file?

If the answer to that is "yes", then could I simply copy and paste ALL my variables from the main.cpp file (used or not) and just add "extern" to the prefix of each one, and remove whatever initially declared values?

And, i think I might have misunderstood. Do the "extern int test" need to be in the .hpp file or the source.cpp file?

And can I have multiple functions within the source.cpp file or just one per source.cpp file as long as I declare each one in the .hpp file?
Last edited on
1. Yes
2. Yes, but only the global ones, obviously, and the 'extern' keyword ONLY goes to .h files. You will require a main.h file.

A little example may be this:

Main.cpp:
1
2
3
4
5
6
7
8
9
10
11
12
13
#include "main.h"
#include <iostream>
#include "test.h"

int main_a = 0;
int main_b = 1;
int main_c = 3;

int main() {
    test();
    cout << main_a << " " << main_b << " " << main_c;
    return 0;
}


Main.h
1
2
3
extern int main_a;
extern int main_b;
extern int main_c;


Test.h
 
void test();


Test.cpp
1
2
3
4
5
6
7
#include "Main.h"
void test()
{
    main_a = 100;
    main_b = 99;
    main_c = 98;
}



This way, Main.h links Main.cpp into Test.cpp.
Same way Test.h links Test.cpp into Main.cpp (function "test").

And can I have multiple functions within the source.cpp file or just one per source.cpp file as long as I declare each one in the .hpp file?

You can have multiple functions, as many as you want.
Same for the 'extern'.
Last edited on
Topic archived. No new replies allowed.