Trying to use getter functions as parameters...

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.

Header class the sword values are stored in:

class swords
{
public:
void setSwordDamage(float strengthEffect, float sharpnessEffect)
{
sworddamage = (strengthEffect + sharpnessEffect)*3;
}
float getDamage()
{
return sworddamage;
}
void setSwordDurability(float x)
{
sworddurability = x;
}
float getSwordDurability()
{
return sworddurability;
}
void setSwordLength(float x)
{
length = x;
}
float getSwordLength()
{
return length;
}
void setSwordWeight(float sworddamage, float sworddurability, float swordlength)
{
swordweight = (sworddamage + sworddurability + swordlength) / 3;
}
float getSwordWeight()
{
return swordweight;
}
private:
float sworddamage;
float sworddurability;
float length;
float swordweight;
};

source.cpp file where the pain begins

int main()
{
long PlayerLevel = 50;
int strength = 5;
int numofattackonweapon;
swords TrainingSword;
TrainingSword.setSwordDamage(strength, 10);
TrainingSword.setSwordDurability(50);
TrainingSword.setSwordLength(2);
TrainingSword.setSwordWeight(getDamage(), getSwordDurability(), getSwordLength());
landenemy Muural;
Muural.SetEnemyDamage(6, 4, 3, PlayerLevel);
std::cout << "Muural deals " << Muural.GetEnemyDamage() << " damage at level 2." << std::endl;
std::cin >> money;
return 0;
}
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:
1
2
3
TrainingSword.setSwordWeight(swordA.getDamage(), 
                             swordB.getSwordDurablity(), 
                             swordA.getSwordLength());


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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
/* 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(float const strengthEffect, float const 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(float const x) { durability = x;    }
  float getDurability() const        { return durability; }
  
  void  setLength(float const 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;
};

Last edited on
Topic archived. No new replies allowed.