• 12
• 12
• 9
• 10
• 13

# [C++] External Scope Problem

This topic is 3563 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hello, I've read a lot about external variables and how they are used. I currently have a header which defines a few variables which are used throughout the program. Now I'm trying to create a list structure to hold info about the players:
//Globals.h
struct Character {

char name[50]; //Player name
int ally; // Friend (0) or Foe (1)
int hp; // Current hitpoints
int hpMax;
int mp; // Current manapoints
int mpMax;
};

list&lt;Character&gt; charList; // List of friends/enemies and their stats


How can I make this list external so multiple files can use it? When I try to access it with
extern list&lt;Character&gt; charList;


Of coarse the Character type is not defined, and defining it would cause linker problems. How can I make charList external?

##### Share on other sites
Are you doing:

#include<globals.h>

in the files that are using that struct?

##### Share on other sites
If I try that I get linker errors for multiple initialization since other files use Globals.h as well... :(
error LNK2005

##### Share on other sites
Thanks, but thats the exact article I've read from to get to where I am. Thats the article I used to read on external variables, but in this case they will not work...

##### Share on other sites
Quote:
 Original post by jeremicThanks, but thats the exact article I've read from to get to where I am. Thats the article I used to read on external variables, but in this case they will not work...

In globals.h write:

extern std::list<Character> charList;

In exactly one source file write:

std::list<Character> charList;

##### Share on other sites
Ok, the easiest way is to put the extern variable in a header file, and in exactly one source file (.cpp), just define the same variable less the extern:

// foo.h#include <vector>class Foo{   // ...};// declarationextern std::vector<Foo> globalFoos;// foo.cpp#include "foo.h"// definitionstd::vector<Foo> globalFoos;

However, it should be noted that global variables are usually indicative of lack of design - or poor design. Usually there are far better alternatives.

Other notes:
Unless you have good reason, consider std::vector as the default container.

Prefer std::string instead of raw char arrays

Instead of commenting ally as Friend (0) or Foe(1), use a boolean:
bool ally;

Or an enum (I would prefer a boolean in this case, but for completeness)
enum State{    ALLIED,    ENEMY,    // could easily add neutral too, or "unknown"};

##### Share on other sites
Thank you, but my list is to be used throughout the whole project. You specified that it can only be used in exactly cpp file, Im trying to use the same list in multiple cpp files...

##### Share on other sites
Quote:
 Original post by jeremicThank you, but my list is to be used throughout the whole project. You specified that it can only be used in exactly cpp file, Im trying to use the same list in multiple cpp files...

Nobody said it can only be used in one file.

What we said was it can only be defined in one file.

By putting the declaration "extern std::list<Character> charList" in global.h, every file that includes globals.h will be able to see that declaration and as a result will be able to use charList.

##### Share on other sites
Alright I think I'm getting somewhere, thanks guys! awesome help! I'm down to one last thing. If Main.cpp defines the structure and the list

//Main.cpp#include "Globals.h"struct Character {		char name[50];	int ally;	int hp;	int hpMax;	int mp;	int mpMax;};list<Character> charList;

And the declaration is stated in the Globals.h

//Globals.hextern list<Character> charList;

The type "Character" is obviously not defined, so where should I define the Character structure so that Globals.h can recognise it wtihout getting multiple definitions, since Main.cpp already includes Globals.h??