Why can't I call a template method from an instance of a derived class in C ++?

Please consider the following types:

struct X
{
    static std::string fullyQualifiedName();
};


struct A
{
    template <class T> void foo()
    {
        return foo(T::fullyQualifiedName());
    }

protected:
    virtual void foo(const std::string& name) = 0;
};


struct B : public A
{
protected:
    void foo(const std::string& name);
};

I have a pointer to instance B, I am trying to call the template version fooas follows:

b->foo< X >();

The compiler complains: "X": illegal use of this type as an expression.

On the other hand, this code works fine:

A* a = b;
a->foo< X >();

Hence my question.

+3
source share
1 answer

, , . , . , B, void B::foo(const std::string& name). , , - , .

, , using:

struct B : A{
   using A::foo;
   void foo( const std::string & name );
};

, , , B, - using , A.

, ( , , , , ):

b->A::foo<X>();
+9

All Articles