Search code examples
c++templatesinherited

Inherited Templates - C++


I have a template base class and an inherited class. There is a function inside the baseclass which can accept difference types, I expected that I would call this from inside the inherited class with 'BaseClass::Add();' but I instead receive the error "expected primary-expression before ‘>’ token".

How do I call BaseClass::Add with U?

template <typename T>
class BaseClass 
{
public:
    template <typename U>
    void Add() {
        // Do stuff
    }
};

template <typename T>
class InheritedClass : public BaseClass<T>
{
public:
    template <typename U>
    void Add() {
        BaseClass<T>::Add<U>(); // Error here
    }
};

Thanks in advance


Solution

  • Use this syntax

    BaseClass<T>::template Add<U>()