cannot access base class members

template<typename T>
class B {
public:
  void f() { }  // Member of class B<T>
};
template<typename T>
class D : public B<T> {
public:
  void g()
  {
    f();  // Bad (even though some compilers erroneously (temporarily?) accept it)
  }
};

/*************
Q:
Why am I getting errors 
when my template-derived-class uses a member it inherits from its template-base-class?

A:
Within D<T>::g(), the name f does not depend on template parameter T, 
so f is known as a nondependent name. 
On the other hand, B<T> is dependent on template parameter T 
so B<T> is called a dependent name.

Here’s the rule: 
the compiler does not look in dependent base classes (like B<T>) 
when looking up nondependent names (like f).

This doesn’t mean that inheritance doesn’t work. 
Class D<int> is still derived from class B<int>, 
the compiler still lets you implicitly do the is-a conversions (e.g., D<int>* to B<int>*), 
dynamic binding still works when virtual functions are invoked, etc. 
But there is an issue about how names are looked up.

Workarounds:
>>
Change the call from f() to this->f(). 
Since this is always implicitly dependent in a template, 
this->f is dependent and the lookup is therefore deferred 
until the template is actually instantiated, 
at which point all base classes are considered.
>>
Insert using B<T>::f; just prior to calling f().
>>
Change the call from f() to B<T>::f(). 
Note however that this might not give you what you want if f() is virtual, 
since it inhibits the virtual dispatch mechanism.

*************/
Source: isocpp.org

C++相关代码片段

how to kill

hello world cpp

cpp hello world

when kotlin

how to write hello world c++

fenwick tree

main function

python loop

is palindrom

subsequence

insertion sort

react cookie

data structure

Stack Modified

increment integer

496. Next Greater Element I.cpp

c++ freecodecamp course 10 hours youtube

simple interest rate

deliberation meaning

fingerprint

c++ system() from variable

operator using

unambiguous

Stream Overloading

quick_sort

hash table

graph colouring

the question for me

fname from FString

how togreper

is plaindrome

logisch oder

robot framework for loop example

spyder enviroment

pallindrome string

ue4 c++ string from fvector

interactive problem

two sum solution

interpolation search

Required Length

new expression

Targon lol

xor linked list

stack implementation

typescript

loop through array

loop array

how to point to next array using pointer c++

file exist

floating point exception

array di struct

castin in C++

varint index

how to make a instagram report bot python

windows servis from console app

add integers

listas enlazadas/ linked lists

niet werkend

bubble sort

triangle angle sum

divisor summation

rotateArray

destiny child

Populating Next Right Pointers in Each Node

cosnt cast

bucket sort

double plus overload

Z-function

binary search

permutation

linked list

Implicit conversion casting

square root

public method

tower of hanoi

selection sort

dangling pointer

hello world programming

statements

volumeof a sphere