Home:ALL Converter>Why am I getting the error 'A<int>' is not an accessible base of 'S<int>' for a base class's base class?

Why am I getting the error 'A<int>' is not an accessible base of 'S<int>' for a base class's base class?

Ask Time:2012-09-23T04:11:20         Author:template boy

Json Formatter

I'm trying to call the class function A<F>::f() from within class S, but I'm getting the following errors when I instantiate an S object ( S<int>s ) and call it's f member function ( s.f() ) :

source.cpp: In instantiation of 'int S<F>::f() [with F = int]':
source.cpp:30:21: required from here
source.cpp:22:25: error: 'A<int>' is not an accessible base of 'S<int>'

Note that this works when I replace return A<F>::f(); inside the declaration of class S with return C<A, F>::f();. But I'm wondering why I can't do it the other way...

#include <iostream>

template <typename T> class A {
   public:
      int f();
};

template <typename T> int A<T>::f() {
   return sizeof(T);
}

template <template <typename> class E, typename D> class C : E<D> {
   public:
      int f() {
         return E<D>::f();
      }
};

template <typename F> class S : C<A, F> {
   public:
      int f() {
         return A<F>::f();
      }
};

int main() {

   S<int>s;

   std::cout << s.f();

}

Any help is appreciated and if you require further clarification please feel free to comment.

Update

Since this questions is resolved I guess I should post the code that actually worked:

#include <iostream>

template <typename T> class A {
   public:
      int f();
};

template <typename T> int A<T>::f() {
   return sizeof(T);
}

template <template <typename> class E, typename D> class C : public E<D> {
   public:
      int f() {
         return E<D>::f();
      }
};

class S : public C<A, int> {};

int main() {

   S s;

   std::cout << s.f(); // 4

}

Author:template boy,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/12547340/why-am-i-getting-the-error-aint-is-not-an-accessible-base-of-sint-for-a
Bo Persson :

template <typename F> class S : C<A, F> \n ^\n\n\nYou don't specify a public inheritance, so it defaults to private, making the base classes inaccessible.",
2012-09-22T20:17:01
Massimiliano :

I think it is just a matter of using public class inheritance. Try with:\n\ntemplate <template<class> class E, typename D> class C : public E<D> {\n public:\n int f() {\n return E<D>::f();\n }\n};\n\ntemplate <typename F> class S : public C<A, F> {\n public:\n int f() {\n return A<F>::f();\n }\n};\n",
2012-09-22T20:15:34
yy