Problem with error LNK2005

I'm trying to use a variable to represent a players health but when I try to use it I get error LNK2005.

in battlesequence.h I have "int health = 100"

1
2
3
4
5
6
7
8
9
10
#include battlesequence.h
  void Battle()
{	
	std::cout << "Player health: " << health << std::endl;
	std::cout << "1. Melee" << std::endl;
	std::cout << "2. Spell" << std::endl;
	std::cout << "3. Item" << std::endl;
	std::cout << "4. Guard" << std::endl;
	std::cout << "5. Flee" << std::endl;
}
battlesequence.h needs quotes:
#include "battlesequence.h"

Also:
in battlesequence.h I have "int health = 100"

That seems like trouble, can you post battlesequence.h?
Last edited on
Sorry, that was a typo. It does have quotes. Here's battlesequence.h

#ifndef TESTINGPROGRAM_TESTINGPROGRAM_BATTLESEQUENCE_H_
#define TESTINGPROGRAM_TESTINGPROGRAM_BATTLESEQUENCE_H_

#include <iostream>

const int swordDmg = 10;
const int spellDmg = 15;
const int potion = 20;
void Battle();
void FightorFlee();
void FleeMessage();
int health = 0;

#endif //TESTINGPROGRAM_TESTINGPROGRAM_BATTLESEQUENCE_H_
multiple definition error http://www.cplusplus.com/forum/beginner/34484/#msg186410

1
2
3
4
5
//header
extern int health; //declaring a global variable that will be shared in the translation units

//in just one source file
int health = 0; //defining the variable 


You may also consider to get rid of the global variable http://c2.com/cgi/wiki?GlobalVariablesAreBad
What is my best option for using something like a health variable that I'll need to use in for instance:

character stats
battle sequence
whatever else

Without using a global variable.

Should I use a class?
Glad I realized I was taking too long to type and did a refresh :)

You are getting a link error ( LNK ), check out the compilation process:
http://faculty.cs.niu.edu/~mcmahon/CS241/Notes/compile.html
Topic archived. No new replies allowed.