Nicholas Halim
Nicholas Halim

Reputation: 11

Why am I getting an undefined reference to a constructor despite not calling it?

This error is beyond my current understanding. I have tried for hours to figure out why this is the case. Can someone help me?

Warrior.h

#include <string>
#ifndef __WARRIOR_H__
#define __WARRIOR_H__
#include "Character.h"

//  HeroType type;
//  string name;
//  double health;
//  double attackStrength;

class Warrior : public Character {
    private:
        string allegiance;
    public:
        Warrior(const string & _name, double _health, double _attackStrength, string _allegiance) : Character(WARRIOR, _name, _health, _attackStrength), allegiance(_allegiance){}
        void attack(Character &);
    
};



#endif

// Warrior::Warrior(const string & _name, double _health, double _attackStrength, string _allegiance) : Character(WARRIOR, _name, _health, _attackStrength), allegiance(_allegiance) 
// {}

Warrior.cpp

#include <iostream>
#include <vector>
#include <cstdlib>
#include "Warrior.h"


using namespace std;

void Warrior::attack(Character &opponent) {
    Warrior &opp = dynamic_cast<Warrior &>(opponent);
    if (opp.allegiance != allegiance)
    {
        double totalDamage;
        totalDamage = (health / MAX_HEALTH);
        opp.damage(totalDamage);
    }
}

// Lecturer::Lecturer(const string & name, 
//                    const string & addr, 
//                    const string & email, 
//                    const string & eid, 
//                    double courseRate, 
//                    double emplPerc)
//  : Employee(name, addr, email, eid), courseRate(courseRate), emplPerc(emplPerc)
// {}

Character.h

#include <string>


using namespace std;

#ifndef __CHARACTER_H__
#define __CHARACTER_H__

enum HeroType {WARRIOR, ELF, WIZARD};

const double MAX_HEALTH = 100.0;

class Character {
 protected:
    HeroType type;
    string name;
    double health;
    double attackStrength;

 public:
    Character(HeroType type, const string &name, double health, double attackStrength);
    HeroType getType() const;
    const string & getName() const;
    int getHealth() const;
    void damage(double d);
    bool isAlive() const;
    virtual void attack(Character &) = 0; //Abstract
 };

#endif

Character.cpp

#include <iostream>
#include <vector>
#include <cstdlib>

using namespace std;

#include "Character.h"


Character::Character(HeroType type, const string &name, double health, double attackStrength) {
    this->type = type;
    this->name = name;
    this->health = health;
    this->attackStrength = attackStrength;
}

HeroType Character::getType() const {
    return type;
}

const string & Character::getName() const {
    return name;
}

int Character::getHealth() const {
    return health;
}

void Character::damage(double d) {
    health = health - d;
}

bool Character::isAlive() const {
    
    return (health > 0);
}

Compiler error

In function Warrior::Warrior(std::string const&, double, double, std::string): main.cpp:(.text._ZN7WarriorC2ERKSsddSs[_ZN7WarriorC5ERKSsddSs]+0x46): undefined reference to Character::Character(HeroType, std::string const&, double, double)

Warrior.o: In function Warrior::attack(Character&): Warrior.cpp:(.text+0x87): undefined reference to Character::damage(double) collect2: error: ld returned 1 exit status

Upvotes: 0

Views: 131

Answers (1)

catnip
catnip

Reputation: 25388

Why am I getting an undefined reference to a constructor despite not calling it?

But you are calling it.

Warrior(const string & _name, double _health, double _attackStrength, string _allegiance) : Character(WARRIOR, _name, _health, _attackStrength), allegiance(_allegiance){}

calls the constructor referenced in the error message, namely:

Character::Character(HeroType type, const string &name, double health, double attackStrength)

As Jeremy says, it looks like you are failing to include Character.cpp in your build, hence the 'undefined reference' errors.

Upvotes: 4

Related Questions