Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

}
like image 704
template boy Avatar asked Sep 22 '12 20:09

template boy


People also ask

How to solve ambiguous error in c++?

You can resolve ambiguity by qualifying a member with its class name using the scope resolution ( :: ) operator. The statement dptr->j = 10 is ambiguous because the name j appears both in B1 and B2 .

What is the need of inheritance in C++?

Inheritance allows us to define a class in terms of another class, which makes it easier to create and maintain an application. This also provides an opportunity to reuse the code functionality and fast implementation time.

Which of the following Cannot be inherited from the base class?

Constructor cannot be inherited but a derived class can call the constructor of the base class.


2 Answers

template <typename F> class S : C<A, F> 
                               ^

You don't specify a public inheritance, so it defaults to private, making the base classes inaccessible.

like image 89
Bo Persson Avatar answered Sep 20 '22 20:09

Bo Persson


I think it is just a matter of using public class inheritance. Try with:

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

template <typename F> class S : public C<A, F> {
   public:
      int f() {
         return A<F>::f();
      }
};
like image 35
Massimiliano Avatar answered Sep 24 '22 20:09

Massimiliano