HANDLE issue

okay, am multi-threading a couple of static class functions, and both functions use a handle i have defined in my header.

here is the ONLY definition i have for the handle:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
//filename database.h
#pragma once

#include<iostream>
#include<string>
#include<vector>
#include<list>
#include<map>
#include<algorithm>
#include<iterator>
#include<locale>
#include<sstream>
#include "stdlib.h"
#include "windows.h"
#include"stdafx.h"

using namespace std;



namespace MUD
{
	HANDLE hHpSetMutex=CreateMutex(NULL,FALSE,NULL) //Mutex for combat loops
	class Item;
	class Room;
	class Player;
	class Mob;
	class Combatants;
	class Dungeon;
	class Filter;
	class Dispatch;
	

but every time i try to compile the code, I get:

NEW_MUD_PROJECT.obj : error LNK2005: "void * MUD::hHpSetMutex" (?hHpSetMutex@MUD@@3PAXA) already defined in database.obj
C:\Users\Alan\Documents\Visual Studio 2010\Projects\NEW_MUD_PROJECT\Debug\NEW_MUD_PROJECT.exe : fatal error LNK1169: one or more multiply defined symbols found.

for reference, the only functions that use the mutex are:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
void Player::attack(Mob* attacked)
{
	if(getCombat()==true)
	{
		cout<<"You are already in combat\n";
	}
	else
	{
		setCombat(true);
		while(death()==false && attacked->death()==false)
		{
			int damage;
			damage=getrandom(mMinDmg,mMaxDmg);
			int damagedone;
			damagedone=damage-(attacked->getDef());
			if(damagedone>0)
			{
				WaitForSingleObject(hHpSetMutex,INFINITE);
				attacked->setHp((attacked->mmHp)-damagedone);
				ReleaseMutex(hHpSetMutex);
				cout<<"You deal "<<damagedone<<" damage to "<<attacked->getName()<<endl;
			}
			else
			{
				cout<<attacked->getName()<<" dodged the attack\n";
			}
			Sleep(((int)(getSwing())*1000));
		}
		setCombat(false);
		if(attacked->death()==true)
		{
			mLocation->mobDeath(attacked->getName());
			cout<<attacked->getName()<<" died!"<<endl;
		}
	}
}

and
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
void Mob::attack(Player* attacked)
{
	if(getCombat()==true)
	{
		cout<<getName()<<" acts like it would like to attack you, but can't seeing as how it is already engaged\n";
	}
	else
	{
		setCombat(true);
		while(death()==false && attacked->death()==false)
		{
			int damage;
			damage=getrandom(mmMinDmg,mmMaxDmg);
			int damagedone;
			damagedone=damage-(attacked->getDef());
			if(damagedone>0)
			{
				WaitForSingleObject(hHpSetMutex,INFINITE);
				attacked->setHp((attacked->mHp)-damagedone);
				ReleaseMutex(hHpSetMutex);
				cout<<getName()<<" deals "<<damagedone<<" damage to you\n";
			}
			else
			{
				cout<<"You dodge the attack\n";
			}
			Sleep(((int)(getSwing())*1000));
		}
		setCombat(false);
		if(attacked->death()==true)
		{
			cout<<attacked->getName()<<" died!"<<endl;
		}
	}
}


these functions are called by the static functions:
1
2
3
4
5
6
7
8
9
10
static void Combatants::initCombatPlayer(void* combatants)
{
	Combatants* fighters=(Combatants*)combatants;
	fighters->getPlayer()->attack(fighters->getMob());
}
static void Combatants::initCombatMob(void* combatants)
{
	Combatants* fighters=(Combatants*)combatants;
	fighters->getMob()->attack(fighters->getPlayer());
}


this error really boggles me, seeing as how there is only one definition of the HANDLE hHpSetMutex, and that is in the header file, so that the functions may use it.
SOLVED.


i set the HANDLE hHpSetMutex definition to static:

 
static HANDLE hHpSetMutex=CreateMutex(NULL,FALSE,NULL);		//Mutex for combat loops 
Topic archived. No new replies allowed.