Jump to content
  • Advertisement
Sign in to follow this  
jeremic

[C++] External Scope Problem

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

If you intended to correct an error in the post then please contact us.

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<Character> 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<Character> charList;

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

Share this post


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

Share this post


Link to post
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 this post


Link to post
Share on other sites
Quote:
Original post by jeremic
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...


In globals.h write:

extern std::list<Character> charList;

In exactly one source file write:

std::list<Character> charList;

Share this post


Link to post
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
{
// ...
};

// declaration
extern std::vector<Foo> globalFoos;

// foo.cpp

#include "foo.h"

// definition
std::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 this post


Link to post
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 this post


Link to post
Share on other sites
Quote:
Original post by jeremic
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...


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 this post


Link to post
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.h

extern 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??

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!