1

This is C++. I have the following program:

#include <iostream>

using namespace std;

template <typename T>
class Base {
public:
    T t;
    void use() {cout << "base" << endl;};
};

template <typename T>
class Derived: public Base<T> {
using Base<T>::use;

public:
   T x;
   void print() { use(); };

};

using namespace std;

int main() {

    Derived<float> *s = new Derived<float>();

    s->Base<float>::use(); // this is okay
    s->use();  // compiler complaints that "void Base<T>::use() is inaccessible"

    s->print(); // this is okay

    return 0;
}

Base::use() does not use the template typename T. According to Why do I have to access template base class members through the this pointer?, I used 'using Base::use' in Derived so I can refer to it as 'use' in Derived::print(). However, I cannot call use() via a pointer to Derived anymore. What would cause this?

Adam
  • 17,838
  • 32
  • 54
B. H.
  • 11
  • 1

1 Answers1

5

You need to have the line

using Base<T>::use;

in the public section of Derived.

template <typename T>
class Derived: public Base<T> {

public:
   using Base<T>::use;
   T x;
   void print() { use(); };

};

Live demo.

R Sahu
  • 204,454
  • 14
  • 159
  • 270