4

Working on a little thing that randomly generates colored blocks. Anyway, for organization, I have each of the generators - with a method, generate() - in their own class, all of which descend from Generator. The World class holds a collection of Generator * to these, and so can be called as generators[randomIndex]->generate().

//in World.h
static std::vector<Generator *> generators;

//in World.cpp
generators.push_back(&Forest());

//Generator.h
class Generator
{
public:
    virtual void generate(sf::Color ** grid, sf::Vector2i size) = 0;
};


//Forest.h
class Forest : Generator
{
public:
        void generate(sf::Color ** grid, sf::Vector2i size);
};

The error:

'type cast' : conversion from 'Forest *' to 'Generator *' exists, but is inaccessible

Why does this occur, and how to fix it?

1 Answer 1

8

You have to inherit publicly:

class Forest : public Generator
//             ^^^^^^
Sign up to request clarification or add additional context in comments.

3 Comments

I was just interested in knowing that is this error apt for this scennario? if yes, how?
@AmanDeepGautam: Yes. Only public inheritance creates an "is-a" relationship upwards.
The inheritance policy sets a bar for everything being inherited and the default, as for a class definition itself, is private. So by leaving out the "public" you are saying "inherit from Generator but mark everything private". Which allows "Forest" to see everything belonging to Generator, but you cannot see it from a Forest-derived class or outside of Forest.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.