I'm trying to separate my code into headers and cpps
I made a .h and .cpp for every class I had
but I have no idea where to put the functions (that aren't in classes) and the global variables
I tried putting them in main but it didn't work
I also tried putting them in every cpp that needed them but I got the error of (Multiple Definition of the variable)
What to do?
Asked
Active
Viewed 91 times
1

Mark Kanemura
- 45
- 4
1 Answers
2
In the .h:
extern int myGlobal;
int myFunction(int arg);
In one of the .cpp files:
int myGlobal;
int myFunction(int arg)
{
return arg + 5;
}
You can split stuff across multiple .cpp files, just don't define anything in more than one place. If you put a definition in a .h file, and include it multiple times, you get multiple (conflicting) definitions.

JasonD
- 16,464
- 2
- 29
- 44