moo
moo

Reputation: 526

C++ calling a inherited method of a templated superclass

I don't understand why the following code does not compile:

#include <iostream>

using namespace std;

template<typename T>
class Base {
public:
    void printTuple(T tuple) {
        std::cout << "Hello1!" << std::endl;
    }
};

template<typename T>
class Derived : public Base<T> {
public:    
    void printTuple(std::string name, T tuple) {
        std::cout << "Hello2!" << std::endl;

        printTuple(tuple);
    }
};

int main()
{
    Derived<int> d1;
    d1.printTuple("Test", 13);

    return 0;
}

I get the following error:

main.cpp:19:25: error: no matching function for call to Derived::printTuple(int&)'

But shouldn't Derived inherit a method with such a signature from Base?

Thanks

Upvotes: 0

Views: 921

Answers (2)

shauryachats
shauryachats

Reputation: 10405

You should change the line printTuple(tuple) to Base<T>::printTuple(tuple) because a function of the base class has been hidden.

Upvotes: 3

John Zwinck
John Zwinck

Reputation: 249602

Just add this to the top of the public part of class Derived:

using Base<T>::printTuple;

This will expose the base class overload of the function, i.e. prevent it from being "shadowed."

Upvotes: 2

Related Questions