How do I define an operator where one operand is a defined type and the other is an int?
I have a classDamage
and defined as such in Damage.h
:
#pragma once
class Damage
{
public:
Damage(int, int = 0, int = 0);
int operator-= (int& num)
{
return num - this->GetDamage();
}
private:
int damage;
int dotDamage;
int dotDamageLength;
};
And I am trying to use the operator like this in Monster.cpp
:
#include "Monster.h"
#include "Damage.h"
int Monster::TakeDamage(Damage& damage)
{
this->health -= damage; // Error Here
return this->health;
}
The error is: "No operator "-=" matches these operands. operand types are int -= damage.
I have also tried using the -
operator too, among my many attempts, defining it outside the class, passing in two different parameters, but nothing has worked yet.