dimanche 2 février 2020

How to fix the "Unresolved External Symbol" Error [duplicate]

I am a beginner programmer working on a C++ exercise called the "RPG Combat Character Framework" on Edabit in order to improve my programming skills. Using the code given in the exercise, I modified it according to its instructions.

This is the code given in the exercise:

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

My code is below:

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

There seems to be no syntax or any other form of errors before I ran my code. However, when I ran the program, I get 14 errors labelled: "LNK2001 Unresolved External Symbol".

enter image description here

I've been looking at the code for the past while and I unfortunately am able to figure out the problem within the code.

With that being said, explain to me what I have done wrong to cause the LNK2001 and LNK2019 errors, and what steps I can take to prevent this from happening next time I program?

Thank you very much,

Aucun commentaire:

Enregistrer un commentaire