2016-03-02 56 views
0

該項目尚未完成,但目前我的團隊遇到了LNK1169錯誤。LNK1169在基本遊戲中找到「一個或多個多重定義的符號」

我們有一個player.h和player.cpp以及一個enemy.h和enemy.cpp,顯然是一個source.cpp。不知何故,當我們結合玩家的工作和處理敵人文件時,文件之間的鏈接就搞砸了。

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); 
// } 
//} 

敵人攻擊::()函數是在進步,而不是一個工作真的是這個問題

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; 
    } 
} 

任何有識之士任何人都可以給到爲什麼LNK1169錯誤可能會彈出將不勝感激。謝謝。

+1

錯誤消息表示您已經定義(至少)一個函數多次。如果沒有更多的信息(例如,完整的編譯器輸出,其中應該包含多次定義的函數),就不可能提供更多的幫助。另外,爲了將來的參考,請務必將您的示例縮小到重現問題所需的最短段。這只是一個代碼轉儲。 –

回答

2

它的簡單,你在Player.h和Enemy.h使用

#ifndef PLAYER_H 
#define PLAYER_H 

兩次。只需簡單地更換:

#ifndef PLAYER_H 
#define PLAYER_H 

通過

在Enemy.h

#ifndef ENEMY_H 
#define ENEMY_H 

還是在的* .h文件

但真正的問題是你的聲明之前使用#pragma once預處理指令Player.h中的此行

std::vector<std::string> type = { "Bronze", "Iron", "Silver", "Steel", "Gold", "Diamond" }; 

要在標題中聲明全局變量,請使用關鍵字extern。

// Player.h 
extern std::vector<std::string> type; 

// Player.cpp 
std::vector<std::string> type = { "Bronze", "Iron", "Silver", "Steel", "Gold", "Diamond" }; 

是不是可以將它改爲枚舉類?

enum class Types { 
    Bronze, 
    Iron, 
    Silver, 
    Steel, 
    Gold, 
    Diamond 
}; 

並通過應用程序使用命名空間?

相關問題