Nested class problem

I want to make methods inside hero class with dynamic memory allocation like initialization, adding, deleting objects, but compiler says that it cannot find declaration.
Error: 'Skill' has not been declared.
Thanks for help in advance.
Error appears in 6th line.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
  class Hero
{
    public:
        Hero();
        Hero(std::string fname);
        void addSkill(Skill *skill);

    protected:

    private:
        class Skill {
            friend class Hero;
            std::string name;
            int dmgMultiplier;
            static int skills_size;
        };

        Skill **skills;
};
Last edited on
At line 6, Skill indeed as not been declared. Just rearrange the class:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class Hero
{
    private:
        class Skill {
            friend class Hero;
            std::string name;
            int dmgMultiplier;
            static int skills_size;
        };

        Skill **skills;
    public:
        Hero();
        Hero(std::string fname);
        void addSkill(Skill *skill);

    protected:

};

addSkill() is public, but it takes the private Skill class as a parameter. How is, say, main() supposed to call addSkill() when it can't even declare a variable of type Hero::Skill?

Also, unless you're doing dynamic memory allocation as an exercise, use vector<> instead. It will save you a ton of headaches.
Last edited on
Thank you so much dhayden. That's helped me a lot.
Topic archived. No new replies allowed.