-2

I have a vector that I would like to store subclasses in. The problem I am facing though is that it is giving me an error. I looked here as a reference but now I am getting this error:

Error: "Subclass::Subclass" (declared at...) is inaccessible.

My code is as follows:

class SuperClass
{
 public:
    SuperClass() {}
    ~SuperClass() {}

};
class SubClass : public SuperClass
{
   SubClass() {} //there is no .cpp
   ~SubClass() {}
};

int main()
{
    std::vector<SuperClass*> superClass;
    superClass.push_back(new SubClass());
    return 0;
}
Community
  • 1
  • 1
Dylan L.
  • 1,243
  • 2
  • 16
  • 35
  • Please post a **complete but minimal** example that readers can try. – Cheers and hth. - Alf Mar 22 '16 at 04:55
  • 1
    That said, the compiler's diagnostic "is inaccessible" says it all. The constructor is inaccessible because members of a `class` are `private` by default. So either use the `struct` keyword or, better here, add `public:` access specifier before the constructor definition, or, ungood, make class `Program` a `friend`. – Cheers and hth. - Alf Mar 22 '16 at 04:57

1 Answers1

1

The default access type for class is private. Hence, both the constructor and the destructor of Subclass are private. Add the public access specifier before them.

class Subclass : public SuperClass{

     // Add this
     public:

        SubClass() {} //there is no .cpp
       ~SubClass() {}
  };
R Sahu
  • 204,454
  • 14
  • 159
  • 270