0
class Base
{
   protected: 
      int data;
   public:
      virtual int getData() { return data; }
      virtual void setData(int value) { data = value; }
};

class Child : protected Base
{
   public:
   void setData(int value)
   {
       Base::setData(value);
       cout << "Data is set.\n";
   }
};

class Worker
{
   private:
      Child obj;
   public:
      void setGlobalData(int val)
      {
         obj.setData(val); // This is normal
      }

      int getGlobalData()
      {
         return obj.getData();  // Line 140, Error
      }
};

Error during compiling of file with Worker class:

Base.hpp: In member function ‘int Worker::getGlobalData()’:
Base.hpp:22:19: error: ‘virtual int Base::getData()’ is inaccessible
Worker.cpp:140:34: error: within this context
Worker.cpp:140:34: error: ‘Base’ is not an accessible base of ‘Child’
Max Frai
  • 61,946
  • 78
  • 197
  • 306

6 Answers6

2

Did you actually make it a public base class?

//            vvvvvv important
class Child : public Base

Otherwise it's private, and you get errors similar to what you have, namely:

‘Base’ is not an accessible base of ‘Child’

GManNickG
  • 494,350
  • 52
  • 494
  • 543
  • Yeah, sorry. There was `protected`. But if I need to hide the `int data`. I don't want to make public inheritance. – Max Frai Oct 20 '10 at 18:04
  • 1
    @Ockonal I think you misunderstand the access modifiers when subclassing. If `int data` is private in the base class, `public`, `protected`, and `private` inheritance do not modify that. It is always `private` to the class that it's a member of. – wkl Oct 20 '10 at 18:07
  • And If I'll make int data as protected there is still error (with protected inheritance). – Max Frai Oct 20 '10 at 18:09
  • @Ockonal: That specified what access users of the class have with respect to the base. The base's specifiers say what access users of that class have to it's members. Regardless of whether or not users can access `Child`'s `Base`, `Base::data` is still `protected`. Changing it to my code should be the only change. If not, please post the new errors/more code. Have you a [book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list)? – GManNickG Oct 20 '10 at 18:10
1

This compiles:

class Base
{
   protected: 
      int data;
   public:
      virtual int getData() {return data;}
      virtual void setData(int value) { data = value; }
};

class Child : public Base
{
   public:
   void setData(int value)
   {
       Base::setData(value);
       cout << "Data is set.\n";
   }
};

class Worker
{
   private:
      Child obj;
   public:
      void setGlobalData(int val)
      {
         obj.setData(val); // This is normal
      }

      int getGlobalData()
      {
         return obj.getData();
      }
};
Donotalo
  • 12,748
  • 25
  • 83
  • 121
0

class Worker not clas Worker

In Worker class, obj.setData(val); is attempting to access a private member of Child class.

dgnorton
  • 2,237
  • 16
  • 12
0

Except for clas -> class and virtua -> virtual your code is perfectly fine. There is absolutely nothing wrong with it. Since literally this doesn't compile because of the typos I suspect your original code was a bit different in that the derivation of Child from Base was private or that getData was private.

Armen Tsirunyan
  • 130,161
  • 59
  • 324
  • 434
0

Since the current existing version of the code has Child subclassing Base via protected inheritance, the public functions in Base become protected in Child, so Worker can not access them via Child object.

wkl
  • 77,184
  • 16
  • 165
  • 176
0

Class Child inherits class Base as protected, so the member function Child::getData() is not publically accessible to clients of Child.

As everyone else here said, changing the inheritance of Base to public is one way to fix it.

class Child: public Base

Also note that casting your Child object to type Base also makes the function Base::getData() of the object publically accessible.

return static_cast<Base *>(&obj)->getData();
David R Tribble
  • 11,918
  • 5
  • 42
  • 52