0

The following code:

#include <iostream>

struct A {
    using A_int = int;               // A_int type declared here
    virtual void foo(A_int) = 0;
};

template <typename T>
struct B : public A {

};

template <typename T>
struct C : public B<T> {
    void foo(A_int) override {
        std::cout << "all good\n";
    }
};

int main()
{
  C<int> c;
  c.foo(1);
}

produces this error:

15:14: error: 'A_int' has not been declared

The same code without the use of templates compiles fine. Could anyone explain why?

user6646922
  • 497
  • 1
  • 6
  • 15

2 Answers2

1

You need to say where A_int comes from:

void foo(typename B<T>::A_int) override {
    std::cout << "all good\n";
}

Here's a demo.

cigien
  • 57,834
  • 11
  • 73
  • 112
0

A_int has to accessed over the class specifier A::, so it becomes A::A_int

#include <iostream>

struct A {
    using A_int = int; 
    virtual void foo(A_int) = 0;
};

template <typename T>
struct B : public A {

};

template <typename T>
struct C : public B<T> {
    void foo(A::A_int) override {
        std::cout << "all good\n";
    }
};


int main()
{
  C<int> c;
  c.foo(1);
}
RoQuOTriX
  • 2,871
  • 14
  • 25