Andrew Goulart
Andrew Goulart

Reputation: 66

Compile Error with Class Inheritance

I am getting a compile error in my PC class on the line where I am setting it's inherited STR stat. The compiler error getting is an "Entity.Stat does not contain a constructor that takes 2 arguments. Now I know this not to be the case, because the base Entity class makes the same declarations in its initialization sequence.

If anyone can take a look to see what I'm doing wrong, that would be great. The StatType item is an ENUM that is declared in another file, and is working without issues.

class PC : Entity {

    private Class job;

    public PC(string name, Race race, Class job, int STR, int DEX, int CON, int WIS, int INT, int CHA){
        this.name = name;
        this.race = race;
        this.job = job;

        // This line here is the line giving me difficulties.
        this.STR = new Entity.Stat(STR, StatType.Strength);
    }

}

public class Entity
{
    protected string name;
    protected Race race;
    protected Stat STR;
    protected Stat DEX;
    protected Stat CON;
    protected Stat WIS;
    protected Stat INT;
    protected Stat CHA;

    public Entity(){ }

    public Entity(string name, Race race, int STR, int DEX, int CON, int WIS, int INT, int CHA){
        this.name = name;
        this.race = race;
        this.STR = new Stat(STR, StatType.Strength);
        this.DEX = new Stat(DEX, StatType.Dexterity);
        this.CON = new Stat(CON, StatType.Constitution);
        this.WIS = new Stat(WIS, StatType.Wisdom);
        this.INT = new Stat(INT, StatType.Intelligence);
        this.CHA = new Stat(CHA, StatType.Charisma);
    }

    private struct Stat
    {
        private int id;
        private int value;
        private int modifier;

        public Stat(int value, StatType id)
        {
            this.id = (int)id;
            this.value = value;
            this.modifier = ((value - 10) / 2);
        }

        public int Value
        {
            get
            {
                return this.value;
            }
            set
            {
                this.value = value;
                this.modifier = ((value - 10) / 2);
            }
        }

        public readonly int Modifier
        {
            get { return this.modifier; }
        }

    }
}

Upvotes: 1

Views: 104

Answers (2)

Daniel Mann
Daniel Mann

Reputation: 59020

Your Stat struct is private, meaning it's only visible to the Entity class, not subclasses. Make it protected and it will be visible to the Entity class and any subclasses.

You can't have readonly properties, either, so you're going to get a compiler error on the line public readonly int Modifier as well.

Upvotes: 2

Haney
Haney

Reputation: 34802

I believe you need to switch Stat to protected:

protected struct Stat

I don't think that the PC class can see the Stat class because it's not visible to it - only to Entity.

Upvotes: 1

Related Questions