Already defined?

I'm getting errors saying that my classes are already defined in other functions. I had previously made a program similar to this but I don't have and I cant find anywhere on the web on how I previously did it. I believe I linked my classes by like having said function for instance...

void getDungeon(Monster &Elites)

but i dont quite remember everything on how to do it...

anyways if it helps heres my error messages and some functions.

Let me know if you need more... I appreciate any help.


1
2
3
4
5
6
7
8
1>Fight.obj : error LNK2005: "public: class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > __thiscall Enemy::getName(void)" (?getName@Enemy@@QAE?AV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@XZ) already defined in Enemy.obj
1>Fight.obj : error LNK2005: "public: void __thiscall Enemy::setName(void)" (?setName@Enemy@@QAEXXZ) already defined in Enemy.obj
1>Fight.obj : error LNK2005: "public: double __thiscall Enemy::getCurrentHP(void)" (?getCurrentHP@Enemy@@QAENXZ) already defined in Enemy.obj
1>Fight.obj : error LNK2005: "public: void __thiscall Enemy::setCurrentHP(double)" (?setCurrentHP@Enemy@@QAEXN@Z) already defined in Enemy.obj
1>Fight.obj : error LNK2005: "public: double __thiscall Enemy::getMaxHP(void)" (?getMaxHP@Enemy@@QAENXZ) already defined in Enemy.obj
1>Fight.obj : error LNK2005: "public: void __thiscall Enemy::setMaxHP(double)" (?setMaxHP@Enemy@@QAEXN@Z) already defined in Enemy.obj
1>Fight.obj : error LNK2005: "public: double __thiscall Enemy::getStrength(void)" (?getStrength@Enemy@@QAENXZ) already defined in Enemy.obj
1>Fight.obj : error LNK2005: "public: void __thiscall Enemy::setStrength(double)" (?setStrength@Enemy@@QAEXN@Z) already defined in Enemy.obj


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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60

#include <iostream>;
#include "Player.h";

using namespace std;

void getMenu();
void setEnemy();

void getMap(){

	char option;
	int const x = 1, y = 1;
	int quests = 0;
	int location[20][20];
	int random = rand() % (6 + 1);

	cout << "Which direction would you like to go (N)orth, (E)ast, (S)outh, (W)est, or view \nthe (M)enu: ";

	cin.get(option);
	cin.ignore(100, '\n');

	option = (toupper(option));

	switch (option){

	case 'N': 
		if (random > 5)
			setEnemy();
		else
			getMap();
		break;
	case 'E':
		if (random > 5)
			setEnemy();
		else
			getMap();
		break;
	case 'S':
		if (random > 5)
			setEnemy();
		else
			getMap();
		break;
	case 'W':
		if (random > 5)
			setEnemy();
		else
			getMap();
		break;
	case 'M':
		getMenu();
		break;
	default:
		cout << "Invalid option! Choose again: \n" << random << endl;
		getMap();

	} // End Switch

};


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
37
38

#include <iostream>;
#include <string>;
//#include "Player.h";
//#include "Enemy.h";

using namespace std;

void setEnemy();

void getFight(){

	setEnemy();

	char move;
	int incorrect = 0;

	do{
		if (incorrect == 0)
			cout << "\nYou encounterd a " << Monst.getName() << "..." << endl;
		else
			cout << "\nInvalid Option! Choose Again. " << endl;
		cout << "(A)ttack or (R)un: " << endl;

		cin.get(move);
		cin.ignore(100, '\n');
		move = (toupper(move));
		
		if ((move != 'A') && (move != 'R'))
			incorrect = 1;
	}while ((move != 'A') && (move != 'R'));



};


I have a feeling you're defining your member functions in a header..
Anyway, how is the code you posted relevant? Post the code where you declare and define Enemy class instead.
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62

#ifndef ENEMY_H
#define ENEMY_H

#include <iostream>;
#include <string>;

using namespace std;

class Enemy{
	
private:
	string Names[8];
	string Name;
	double CurrentHP;
	double MaxHP;
	double Strength;
public:
	string getName();
	double getCurrentHP(), getMaxHP(), getStrength();
	
	 void setName();
	 void setCurrentHP(double CurrentHP);
	 void setMaxHP(double MaxHP);
	 void setStrength(double Strength);
};

 string Enemy::getName(){
	 return Name;
 }

 void Enemy::setName(){
	 int random = (rand() % (8 + 1));
	 string Names[8] = {"Wolf", "Evil Bunny", "Donkey", "Serpent", "Dragon Whelp", "Eagle", "Crazed Peasant", "Imp"};
	 this->Name = Names[random];
 }

 double Enemy::getCurrentHP(){
	 return CurrentHP;
 }

 void Enemy::setCurrentHP(double CurrentHP){
	 this->CurrentHP = CurrentHP;
 }

 double Enemy::getMaxHP(){
	 return MaxHP;
 }

 void Enemy::setMaxHP(double MaxHP){
	 this->MaxHP = MaxHP;
 }

 double Enemy::getStrength(){
	 return Strength;
 }

 void Enemy::setStrength(double Strength){
	 this->Strength = Strength;
 }

#endif ENEMY_H 
either:

1) put the function bodies in a cpp file (not a header)

or

2) make the functions inline (ie: inline void Enemy::setMaxHP(...){)

or

3) move the function bodies so that they are inside the class
Topic archived. No new replies allowed.