0

I currently have a few header files. One named "globalVar.h" for global variables, one named "generate.h" for variables having to do with generation, and I have one named "screen.h" for variables having to do with the screen. Whenever I include these and run the file I get an error exactly like this one for each of my variables:

globalVar.h:7:19: warning: 'y' initialized and declared 'extern'
 extern int x = 1, y = 1; // player coordinates

Why is this happening? For this example, here's the code for the "globalVar.h" header:

#ifndef GLOBALVAR_H
#define GLOBALVAR_H

#include <iostream>
#include <windows.h>

extern int x = 1, y = 1; // player coordinates
extern int health = 100; // player health
extern int player = '@'; // player symbol

extern int gX, gY; // goblin coordinates
extern int health = 100; // goblin health
extern int goblin = 'G'; // goblin symbol

#endif

Am I doing something wrong? Surely I am because this makes sense to me, but I don't really know how header files work.

Anyone have any ideas?

tadman
  • 208,517
  • 23
  • 234
  • 262
Gavin C.
  • 105
  • 8
  • Stay **away** from global variables. Problems like this completely disappear when you do that. These should be contained within some kind of `struct` or `class` that's created *and used* in a particular scope. – tadman Apr 15 '21 at 03:39
  • 1
    Hint: When you declare something `extern` you're saying "there's this thing defined elsewhere". You're saying "it's defined elsewhere, and it's also this value" which is a contradiction. – tadman Apr 15 '21 at 03:40
  • 1
    Does this answer your question? [How do I use extern to share variables between source files?](https://stackoverflow.com/questions/1433204/how-do-i-use-extern-to-share-variables-between-source-files) – 273K Apr 15 '21 at 03:40
  • @tadman do you know any references to start learning about those because I don't even know where to begin. And I guess I never really thought about what `extern` meant. Thanks for your help though. :) – Gavin C. Apr 15 '21 at 03:43
  • [The Definitive C++ Book Guide and List](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) – user4581301 Apr 15 '21 at 03:44
  • It's absolutely critical that you have a [good C++ reference book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) on hand at all times when learning C++. There's so many aspects of this language you'll need to pick up before you can put that book away. For a free, online version, start with [cppreference](https://en.cppreference.com/w/). – tadman Apr 15 '21 at 03:44
  • Tip: Define `class mob { int x, y; int health; int symbol; }` instead of this mess of variables. You can then define `mob::mob()` and such to initialize it accordingly, and even subclass as `player` if you want other behaviours. That's how C++ typically plays out. Note that this provides *encapsulation*. In your original code you have two variables called `health` that collide, which is a classic example of how global variables are almost always more trouble than they're worth. – tadman Apr 15 '21 at 03:53
  • I think this provides part of the answer https://stackoverflow.com/q/66414899/7733418 though the "extern" discussion in the proposed duplicate goes a long way. – Yunnosch Apr 15 '21 at 06:11
  • @tadman thank you for you're help. Also, just now realized that health is stated twice. – Gavin C. Apr 15 '21 at 13:52

0 Answers0