So I am trying to do this, and to be fair I'm not even entirely sure at this point if this is something I can properly do, so I'm not sure if there is an error on my part where I missed something while coding or just cannot do this entirely. Anyway, the error is at the setswordweight function parameters where I used getter functions in place of the parameters, and the error I'm getting is Identifier <insert getter function name here> is undefined.
It is very hard to read your code how you have it written. I assume you copy pasted it from whatever program you are using, but you should use the code /code formatting, or an outside source to make it easier to read.
For your specific problem, you need to call getters on the class object.
Simply calling getDamage will not do anything. you need to call it on the object. Such as: TrainingSword.getDamage(). This will return the sworddamage variable.
Here: TrainingSword.setSwordWeight(getDamage(), getSwordDurability(), getSwordLength());getDamage() requires context --- from which sword do you want to get the damage? Example:
However, this implies that a sword's weight is a function of another other sword's damage, another (not mutually exclusive) sword's durability, and yet another sword's length. That seems fundamentally wrong -- the sword's weight is a function of it's own damage, durability, and length, then you do not need to ever set it directly.
/* Not "class swords", class sword, singular. You are defining
a "blueprint" for every specific sword. */
class Sword {
public:
/* I think this is okay, but it's still questionable -- I would expect that a
"strength effect" doesn't belong to the sword, and if this was the case
then this is bad design for the same reason that setWeight() is: why
does the caller need to remember to update the sword's damage if the
strength effect expires? */
void setDamage(floatconst strengthEffect, floatconst sharpnessEffect) {
damage = (strengthEffect + sharpnessEffect) * 3;
}
/* Accessors do not change the object they're accessing. They should be
marked `const'. */
float getDamage() const { return damage; }
/* You won't be changing the value of `x' in the function body. Mark it
`const': this is to help you avoid bugs, not the caller. */
void setDurability(floatconst x) { durability = x; }
float getDurability() const { return durability; }
void setLength(floatconst x) { length = x; }
float getLength() const { return length; }
/* The weight is a function of the properties of a sword. Not of other
swords, but this one. You'll never need to set it directly -- compute it
when required. The alternative is bad, because it shouldn't be up to the
caller to remember to update the sword's weight as things change. */
float getWeight() {
return (getDamage() + getDurability() + getLength()) / 3.0f;
}
private:
float damage;
float durability;
float length;
};