0

I just started coding in C++ and am trying to figure out how the classes and objects work. I have declared a class and object and tried calling functions on the object, but I get a undefined refrence error. Here is my code

Main file where I am creating the object (main.cpp)

#include "enemy.h"

int main()
{
    Enemy zombie;
    zombie.health = 10;
    zombie.takeDamage(2);
}

Header file for enemy class (enemy.h)

#ifndef CITY_H_INCLUDED
#define CITY_H_INCLUDED

class Enemy
{
public:
    int health;

    void takeDamage(int dmg);
};

#endif // CITY_H_INCLUDED

cpp file for enemy class (enemy.cpp)

#include "enemy.h"

void Enemy::takeDamage(int dmg)
{
    damage -= dmg;
}

When I run it I get an error on the line

zombie.takeDamage(2);

It says: undifined refrence to Enemy::takeDamage(int) I have checked everywhere and I have no idea whats wrong with my code.

0 Answers0