找到一个或多个多重定义的符号,错误LNK1169 C ++

时间:2015-08-22 06:16:17

标签: c++ visual-studio visual-studio-2013 linker linker-errors

很抱歉缺少描述性标题。 我收到错误"找到一个或多个多重定义的符号&#34 ;;更具体地说,我得到了46个。一个用于头文件/源文件中的每个函数。使用Visual Studios 2013。

以下是相关的源代码:

augments.h

#ifndef AUGMENTS_H_
#define AUGMENTS_H_

namespace Augments {
  // only used for pass-ins for constructors
  enum class Weapon_Type {
    sword
  };

  enum class Head_Type {
    head
  };

  enum class Body_Type {
    body
  };

  // the player (Monster) has a head, body and weapon
  // that augments his abilities. This will mostly
  // be the thing that draws to the screen for each
  // augmentation, but more importantly it contains
  // specific statistics about each one.

  // So if there was a body that was able to fly,
  // there would be a bool that denotes this ability.

  class Head : public Actor {
    const Head_Type type;
  public:
    Head(Head_Type);
    void Update(float dt);
  };

  class Body : public Actor {
    const Body_Type type;
  public:
    Body(Body_Type);
    void Update(float dt);
  };

  class Weapon : public Actor {
    const Weapon_Type type;
  public:
    Weapon(Weapon_Type);
    void Update(float dt);
  };
}

using AugHead = Augments::Head;
using AugBody = Augments::Body;
using AugWep  = Augments::Weapon;

#endif

augments.cpp

#include "stdafx.h"
#include "Angel.h"
#include "Augments.h"
#include "LD33.h"

Augments::Head::Head(Augments::Head_Type x) : type(x) {
  switch ( x ) {
    case Head_Type::head:
      SetSprite("Images\\head.png");
    break;
  };
};

void Augments::Head::Update(float dt) {
  SetPosition(Game::thePlayer->GetPosition().X,
              Game::thePlayer->GetPosition().Y-
                MathUtil::ScreenToWorld(Vec2i(0,40)).Y);
};

Augments::Body::Body(Augments::Body_Type x) : type(x) {
  switch ( x ) {
    case Body_Type::body:
      SetSprite("Images\\body.png");
    break;
  };
}

void Augments::Body::Update(float dt) {
  SetPosition(Game::thePlayer->GetPosition().X,
              Game::thePlayer->GetPosition().Y);
}

Augments::Weapon::Weapon(Augments::Weapon_Type x ) : type(x) {
  switch ( x ) {
    case Weapon_Type::sword:
      SetSprite("Images\\weapon.png");
    break;
  }
}

void Augments::Weapon::Update(float dt) {
  SetPosition(Game::thePlayer->GetPosition().X,
              Game::thePlayer->GetPosition().Y-
                MathUtil::ScreenToWorld(Vec2i(0,-40)).Y);
}

monster.h

// monster related

#include "Augments.h"
#include "Angel.h"

#ifndef MONSTER_H_
#define MONSTER_H_

namespace Player {
  // contains information like health attack etc
  // but more important body types
  class Monster : public PhysicsActor {
    int max_health, curr_health;
    int attack_damage;
    Augments::Head* frame_head;
    Augments::Weapon* frame_weapon;
    Augments::Body* frame_body;
  public:
    void Refresh(float dt);

    int R_Max_Health() const;
    int R_Curr_Health() const;
    int R_Attack_Damage() const;

    Augments::Head* R_Frame_Head();
    Augments::Weapon* R_Frame_Weapon();
    Augments::Body* R_Frame_Body();


    void Set_Max_Health(int);
    void Set_Curr_Health(int);
    void Add_Curr_Health(int);
    void Set_Attack_Damage(int);
    // will automatically remove old
    // actors from the stage and deallocate
    void Set_Frame_Head(Augments::Head_Type);
    void Set_Frame_Weapon(Augments::Weapon_Type);
    void Set_Frame_Body(Augments::Body_Type);


    Monster(Augments::Head_Type,
            Augments::Weapon_Type,
            Augments::Body_Type);
  };

};

using PlMonster = Player::Monster;


#endif

monster.cpp

#include "stdafx.h"
#include "Monster.h"
#include "Augments.h"
#include "Angel.h"

void Player::Monster::Refresh(float dt) {


};

int Player::Monster::R_Max_Health()    const { return max_health; }
int Player::Monster::R_Curr_Health()   const { return curr_health; }
int Player::Monster::R_Attack_Damage() const { return attack_damage; }

Augments::Head*   Player::Monster::R_Frame_Head()    { return frame_head; }
Augments::Body*   Player::Monster::R_Frame_Body()    { return frame_body; }
Augments::Weapon* Player::Monster::R_Frame_Weapon()  { return frame_weapon; }


void Player::Monster::Set_Max_Health(int x)  { max_health = x; }
void Player::Monster::Set_Curr_Health(int x) { curr_health = x; }
void Player::Monster::Add_Curr_Health(int x) { curr_health += x; }
void Player::Monster::Set_Attack_Damage(int x) { attack_damage = x; }

void Player::Monster::Set_Frame_Head(Augments::Head_Type x) {
  if ( frame_head != nullptr ) {
    frame_head->Destroy();
    delete frame_head;
    frame_head = nullptr;
  }
  frame_head = new Augments::Head(x);
  theWorld.Add(frame_head);
};
void Player::Monster::Set_Frame_Weapon(Augments::Weapon_Type x) {
  if ( frame_weapon != nullptr ) {
    theWorld.Remove(frame_weapon);
    delete frame_weapon;
    frame_weapon = nullptr;
  }
  frame_weapon = new Augments::Weapon(x);
  theWorld.Add(frame_weapon);
};
void Player::Monster::Set_Frame_Body(Augments::Body_Type x) {
  if ( frame_body != nullptr ) {
    theWorld.Remove(frame_body);
    delete frame_body;
    frame_body = nullptr;
  }
  frame_body = new Augments::Body(x);
  theWorld.Add(frame_body);
};


Player::Monster::Monster(Augments::Head_Type head,
                  Augments::Weapon_Type weapon,
                  Augments::Body_Type body) {
  frame_body = nullptr;
  frame_head = nullptr;
  frame_weapon = nullptr;
  Set_Frame_Body(body);
  Set_Frame_Head(head);
  Set_Frame_Weapon(weapon);
}

source.cpp

#include "stdafx.h"
#include "LD33.h"
#include "Angel.h"


int main ( ) {
  Game::Initialize();
  theWorld.StartGame();
  theWorld.Destroy();
  return 0;
}

错误包括以下内容:

Error   43  error LNK2005: "public: int __thiscall Player::Monster::R_Attack_Damage(void)const " (?R_Attack_Damage@Monster@Player@@QBEHXZ) already defined in Augments.obj  C:\Users\The Shire\Desktop\ludum_dare\ludumdare33\Code\ClientGame\source.obj    ClientGame

Error   3   error LNK2005: "public: void __thiscall Player::Monster::Set_Frame_Body(enum Augments::Body_Type)" (?Set_Frame_Body@Monster@Player@@QAEXW4Body_Type@Augments@@@Z) already defined in Augments.obj   C:\Users\The Shire\Desktop\ludum_dare\ludumdare33\Code\ClientGame\LD33.obj  ClientGame

对于Monster中的每一个函数来说,它几乎都是相似的。

很抱歉只是倾倒了大量的代码。我一直试图调试它一段时间,但无法提出任何合理的解决方案。我有包含警卫,我没有看到任何命名空间冲突,并且需要定义的所有内容都在头文件之外。我无法在augments.h中找到导致此问题的任何内容。我也认为它可能是枚举,但用int替换它们也不起作用。

我认为它可能是包含文件的顺序?哼。也许某处有多个源文件,但我不知道如何。有什么帮助吗?

1 个答案:

答案 0 :(得分:0)

在事故中将Monster.cpp包含在另一个头文件中!糟糕