Adding and Subtracting to a global int

How would I go about declaring a global int in a header file, then manipulating it via addition and subtraction in the .cpp file. Every time I tried "health + 10" it returned an output of 0; yet when I used health++, or health = x, it would output correctly. Also, is declaring a global int in a header file to be manipulated in the .cpp file good practice?



This is an example .cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <string>
#include "Main.h"


testing::testing(){
	std::cout << health << std::endl;
	return;
}


int main() {

	health + 20;
	int increment(0);

			testing();
			std::cin >> increment;

	}


and the example header
1
2
3
4
5
6
7
8
9
10
11
#pragma once
static int health = 0;


class testing {


public:
	testing();

};
> health + 20;
statement has no effect. You make an operation and discard the result.


if you declare static int header; then each cpp would have its own copy of the variable
if you declare extern int header; then they'll all work on the same variable (it must be defined once in only one cpp)
The error was that I was using "+" instead of "=+", whoopsie. Thanks for the info ne555
Topic archived. No new replies allowed.