Reputation: 19
This project is not done but currently my group is stuck with the LNK1169 error.
We have a player.h and player.cpp as well as an enemy.h and enemy.cpp and obviously a source.cpp. Somehow the linking between the files got messed up when we combined work on the player and work on the enemy files.
Source.cpp
//#pragma once
#include "Player.h"
#include "Enemy.h"
#include <iostream>
#include <fstream>
#include <string>
#include <time.h>
#include <random>
using namespace std;
int main()
{
cout << "Welcome to our game" << endl;
cout << endl << endl << endl;
int ans = 0;
do {
cout << " Main Menu" << endl;
cout << "-----------------------------" << endl;
cout << "1: Play Game" << endl;
cout << "-----------------------------" << endl;
cout << "2: Exit" << endl;
cout << "-----------------------------" << endl;
cin >> ans;
switch (ans)
{
case 1: //main body of game
case 2:
return 0;
default:
cout << "Please enter 1 to play the game or 2 to exit" << endl;
cin >> ans;
break;
}
} while (ans != 2);
return 0;
}
Enemy.h:
/* UML
Enemies
******************************************
Private
- Health: int
- Attack : int
- Defence : int
******************************************
Public
+ accessor and mutator functions
+ AttackCharacter()
+ DefendCharacter()
+ ChangePosition()
+ LoseHealth()
+ RandomSpawn()
*******************************************
*/
//#pragma once
#ifndef PLAYER_H
#define PLAYER_H
#include <iostream>
#include <string>
using namespace std;
class Enemy
{
private:
int
health,
attack,
defence;
public:
Enemy(int Health, int Attack, int Defence)
{
health = Health; attack = Attack; defence = Defence;
}
int getHealth();
int getAttack();
int getDefence();
void setHealth(int h);
void setAttack(int a);
void setDefence(int d);
//void Attack(Player P1);
};
#endif
Enemy.cpp
#include "Enemy.h"
#include "Player.h"
/*#include <iostream>
#include <string>
using namespace std;
*/
int Enemy::getHealth() { return health; }
int Enemy::getAttack() { return attack; }
int Enemy::getDefence() { return defence; }
void Enemy::setHealth(int h)
{
health = h;
}
void Enemy::setAttack(int a)
{
attack = a;
}
void Enemy::setDefence(int d)
{
defence = d;
}
//void Enemy::Attack(Player P1)
//{
// int h = P1.getHealth();
// int d = P1.getDefence();
// int a = getAttack();
// if (d + h - a > h)
// {
// cout << "You lost 0 health" << endl;
// P1.setHealth(h);
// }
// else
// {
// int h1 = h + d - a;
// cout << "You lost " << h1 - h << " health" << endl;
// P1.setHealth(h1);
// }
//}
The Enemy::attack() function is a work in progress and not really the problem
Player.h:
//#pragma once
#ifndef PLAYER_H
#define PLAYER_H
#include <iostream>
#include <vector>
#include <string>
using namespace std;
struct Armor
{
string name;
char type;
int health;
int attack;
int defense;
Armor() { name = ""; type = ' '; health = 0; attack = 0; defense = 0; } // constructor
};
struct Weapon
{
string name;
char type;
int health;
int attack;
int defense;
Weapon() { name = ""; type = ' '; health = 0; attack = 0; defense = 0; } // constructor
};
struct Shield
{
string name;
char type;
int health;
int attack;
int defense;
Shield() { name = ""; type = ' '; health = 0; attack = 0; defense = 0; } // constructor
};
struct Potion
{
string name;
char type;
int health;
int attack;
int defense;
Potion() { name = ""; type = ' '; health = 0; attack = 0; defense = 0; } // constructor
};
vector<string> type = { "Bronze", "Iron", "Silver", "Steel", "Gold", "Diamond" };
class Player
{
private:
string name;
int initialhealth;
int initialattack;
int initialdefense;
int health;
int attack;
int defense;
public:
Player(string n = " ", int ih = 0, int ia = 0, int id = 0, int h = 0, int a = 0, int d = 0)
{
name = n; initialhealth = ih; initialattack = ia; initialdefense = id; health = h; attack = a; defense = d;
};
Armor armor;
Weapon weapon;
Shield shield;
Potion potion;
string getname();
int getinitialhealth();
int getinitialattack();
int getinitialdefense();
int getHealth();
int getAttack();
int getDefense();
void setname(string n);
void setinitialhealth(int ih);
void setinitialattack(int ia);
void setinitialdefense(int id);
void setHealth(int h);
void setAttack(int a);
void setDefense(int d);
void addITEMS();
void displayPlayer();
void checkARMOR();
};
#endif
Player.cpp:
//#include <iostream>
//#include <string>
#include "Player.h"
//using namespace std;
string Player::getname() { return name; }
int Player::getinitialhealth() { return initialhealth; }
int Player::getinitialattack() { return initialattack; }
int Player::getinitialdefense() { return initialdefense; }
int Player::getHealth() { return health; }
int Player::getAttack() { return attack; }
int Player::getDefense() { return defense; }
void Player::setname(string n) { name = n; }
void Player::setinitialhealth(int ih) { initialhealth = ih; }
void Player::setinitialattack(int ia) { initialattack = ia; }
void Player::setinitialdefense(int id) { initialdefense = id; }
void Player::setHealth(int ih) { health = ih; }
void Player::setAttack(int ia) { attack = ia; }
void Player::setDefense(int id) { defense = id; }
void Player::addITEMS()
{
health = initialhealth + armor.health + weapon.health + shield.health;
attack = initialattack + armor.attack + weapon.attack + shield.attack;
defense = initialdefense + armor.defense + weapon.defense + shield.defense;
}
void Player::displayPlayer()
{
cout << endl;
cout << "=========================" << endl;
cout << " Name : " << name << endl;
cout << " Health : " << health << endl;
cout << " Attack : " << attack << endl;
cout << " Defence : " << defense << endl;
cout << "=========================" << endl;
}
void Player::checkARMOR()
{
if (weapon.name == "Bronze")
{
armor.health = 10;
armor.attack = 5;
armor.defense = 15;
}
if (armor.name == "Iron")
{
armor.health = 100;
armor.attack = 15;
armor.defense = 150;
}
}
Any insight anyone could give into why the LNK1169 error may be popping up would be greatly appreciated. Thank you.
Upvotes: 0
Views: 697
Reputation: 516
Its simple, you have used
#ifndef PLAYER_H
#define PLAYER_H
twice in Player.h and Enemy.h. Just simply replace:
#ifndef PLAYER_H
#define PLAYER_H
by
#ifndef ENEMY_H
#define ENEMY_H
in Enemy.h
Or use #pragma once
preprocessor directive before your declarations in *.h
files
But the real problem is this line in Player.h
std::vector<std::string> type = { "Bronze", "Iron", "Silver", "Steel", "Gold", "Diamond" };
To declare an global variable in a header use the keyword extern.
// Player.h
extern std::vector<std::string> type;
// Player.cpp
std::vector<std::string> type = { "Bronze", "Iron", "Silver", "Steel", "Gold", "Diamond" };
Is it not an option to change this to an enum class?
enum class Types {
Bronze,
Iron,
Silver,
Steel,
Gold,
Diamond
};
And use namespace through out the application?
Upvotes: 2