如何解决“无法解析的外部符号”错误

时间:2020-02-02 16:58:21

标签: c++ c++11 lnk2001

我是一名初学者程序员,从事Edabit上称为"RPG Combat Character Framework"的C ++练习,目的是提高我的编程技能。使用练习中给出的代码,我根据其说明对其进行了修改。

这是练习中给出的代码:

class Char_base
{
protected:
  virtual void SetHP(int hp);
  virtual void SetAtk(int atk);
  virtual void SetDef(int def);
  virtual void SetLevel(int lvl);

public:
  const std::string name;
  Char_base(std::string Name);
  ~Char_base();
  virtual int GetHP() const;
  virtual int GetAtk() const;
  virtual int GetDef() const;
  virtual int GetLevel() const;
  virtual bool IsDead() const;
  virtual void Victory();
  void Attack(Char_base &other);
  void Load(int hp, int atk, int def, int lvl);
};

///////////////////////////////////////////
// Your Code

class Character : public Char_base
{
  int _hp;
  int _atk;
  int _def;
  int _lvl;

public:
  Character(std::string Name);
  ~Character();
};

class Player : public Character
{
  int _xp;

public:
  Player(std::string Name = "Player");
  ~Player();
};

class Enemy : public Character
{
  int _xpReward;

public:
  Enemy(std::string Name = "Enemy");
  ~Enemy();
};

我的代码如下:

#include <iostream>

using namespace std; 

/*------------------ DO NOT MODIFY --------------------------------------------------*/
class Char_base
{
protected:
    virtual void SetHP(int hp); // done
    virtual void SetAtk(int atk); // done 
    virtual void SetDef(int def); // done 
    virtual void SetLevel(int lvl);  // done

public:
    const std::string name;  // name of character 
    Char_base(std::string Name);  // constructor 
    ~Char_base();   // destructor 
    virtual int GetHP() const; // done
    virtual int GetAtk() const; // done 
    virtual int GetDef() const; // done 
    virtual int GetLevel() const; // done 
    virtual bool IsDead() const;
    virtual void Victory();
    void Attack(Char_base& other); // done
    void Load(int hp, int atk, int def, int lvl);  // done
};

// Implementations of Char_base
Char_base::Char_base(string Name) : name(Name) { /* Default constructor */ }
Char_base::~Char_base() { /* Default destructor */ }

/*------------------ DO NOT MODIFY --------------------------------------------------*/


class Character : public Char_base
{
    int _hp;
    int _atk;
    int _def;
    int _lvl;

    string name;

public:
    // Constructor and destructor for character
    Character(string Name) : Char_base(Name) {};
    ~Character();

    // Setters 
    void SetHP(int hp) { _hp = hp; }
    void SetAtk(int atk) { _atk = atk;  }
    void SetDef(int def) { _def = def; }
    void SetLevel(int level) { _lvl = level;  }

    // Getters
    int GetHP() const { return _hp; }
    int GetAtk() const { return _atk;  }
    int GetDef() const { return _def;  }
    int GetLevel() const { return _lvl; }
    string GetName() const { return name; }

    void Attack(Char_base& other) { // interaction when char1 attacks char2
        cout << "stub";
    }

    void Load(int hp, int atk, int def, int lvl) { 
        cout << "stub";
    }

    virtual bool IsDead() const { return false; }
    virtual void Victory() { cout << "stub";  }

};



class Player : public Character
{
    int _xp;

public:
    Player(int health_points, int defense, int attack, string Name = "Player") : Character(Name) {

        Character::SetLevel(1);
        Character::SetDef(defense);
        Character::SetAtk(attack);
        Character:SetHP(health_points);
    };
    ~Player();
};



class Enemy : public Character
{
    int _xpReward;

public:
    Enemy(int level, int health_points, int defense, int attack, string Name = "Enemy"): Character(Name) {
        Character::SetLevel(level);
        Character::SetDef(defense);
        Character::SetAtk(attack);
        Character:SetHP(health_points);
    }
    ~Enemy();
};


// Function that returns information 
void character_info(Character chara) {
    /* Create array to hold character info
    0 -> holds hp
    1 -> holds atk
    2 -> def
    3 -> level */
    int info[4] = { chara.GetHP(), chara.GetAtk(), chara.GetDef(), chara.GetLevel() };
    string units[4] = { "hit points", "attack", "defense", "level" };

    // loop through info 
    for (int i = 0; i < 4; i++)
        cout << chara.GetName() << " has " << info[i] << units[i] << endl;

    cout << "\n";
}

void createGameplaySession(int hp, int atk, int def, int lvl) {  // creates gameplay session 

        // Create player 
    Player player_one(hp, atk, def, "Player One");

    // Create a level 1 enemy with 5 hp, 5 def, and 5 attack 
    Enemy enemy_one(1, 5, 5, 5, "Enemy One");

    // Return information
    character_info(player_one);
    character_info(enemy_one);
}


/*------------------ MAIN --------------------------------------------------*/
int main() {

    createGameplaySession(10, 10, 10, 1);

    return 0;
}

在我运行代码之前,似乎没有语法或任何其他形式的错误。但是,当我运行该程序时,出现14个标记为“ LNK2001 Unresolved External Symbol”的错误。

enter image description here

过去一段时间,我一直在查看代码,但不幸的是我能够找出代码中的问题。

话虽如此,请向我解释导致LNK2001和LNK2019错误的错误操作,以及在我下次编程时可以采取哪些措施来防止这种情况发生?

非常感谢您

0 个答案:

没有答案