I'm confused about how to define and declare my variables and functions across multiple files WITHOUT resorting to global variables. Let's say I wanted to have separate header and source files declaring and defining variables and functions outside of 'main.cpp', to be used in 'main.cpp'.
EDIT: Apologies for my unclear example, but there will only ever be one balloon. I don't want balloon to be an object. It's just to hold some variables and functions.
//balloon.h
bool inflated = true;
void pop();
-
//balloon.cpp
#include "balloon.h"
void pop()
{
inflated = false;
}
-
//main.cpp
#include "balloon.h"
int main()
{
pop();
return 0;
}
If I do this it gives me errors for having multiple definitions of 'inflated', and that it was first declared in 'balloon.cpp'.
If I use 'extern', it works, but gives me warnings about initializing and declaring 'inflated' in 'balloon.h'.
If I decide not to define inflated in balloon.h, it gives me errors about an undefined reference to 'inflated'.
What is the standard way of going about this? Am I missing some key information of variable/function declaration/definition across multiple files?
Straight away it should be
//balloon.h
extern bool inflated;
void pop();
//balloon.cpp
#include "balloon.h"
bool inflated = true;
void pop() {
inflated = false;
}