c++ - LNK1169 "one or more multiply defined symbols found"在基本游戏中

标签 c++ project

此项目尚未完成,但目前我的小组遇到了 LNK1169 错误。

我们有一个 player.h 和 player.cpp 以及一个 enemy.h 和 enemy.cpp,显然还有一个 source.cpp。当我们将对播放器的工作与对敌人文件的工作结合起来时,文件之间的链接不知何故变得困惑。

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

敌人.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

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

Enemy::attack() 函数正在开发中,并不是真正的问题

播放器.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

播放器.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 错误,我们将不胜感激。谢谢。

最佳答案

很简单,你用过

#ifndef PLAYER_H
#define PLAYER_H

两次出现在 Player.h 和 Enemy.h 中。只需简单地替换:

#ifndef PLAYER_H
#define PLAYER_H

通过

#ifndef ENEMY_H
#define ENEMY_H

在 Enemy.h 中

或者在 *.h 中的声明之前使用 #pragma once 预处理器指令 文件

但真正的问题是Player.h中的这一行

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

要在 header 中声明全局变量,请使用关键字 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
};

并在整个应用程序中使用命名空间?

关于c++ - LNK1169 "one or more multiply defined symbols found"在基本游戏中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35757682/

相关文章:

python-3.x - Python - Jira 使用/rest/api/2 创建具有共享配置的项目(现有项目)

c - 关于 Visual Studio 项目文件的问题

sharepoint-2010 - 您的客户端不支持使用Windows资源管理器打开此列表

python - Dlib 人脸检测在 C++ 上表现糟糕,在 Python 上表现不错,为什么?

C++内存对齐问题

c++ - 科学记数法对于 C 中的整数常量是否安全?

c++ - 使用函数构造 std::thread

c++ - 如何使用来自boost的模板相关信号成员实现模板类?

google-cloud-platform - GCP - 查找哪些项目正在使用以及哪些项目为空且未使用

c++ - 安装独立的 visual-C++ 项目属性