Compiler error when trying to call a template method from a private instance

(This question is only a duplicate of another question if you already know the answer!)

(Pay attention to my next question: Why is the template keyword not required if there is an unrelated global template function with the same name? )

I get a compiler error on the specified line when I try to compile C ++ boilerplate code with this structure:

template <int N> struct A { template <int i> void f() {}; }; template <int N> struct B { A<N> a; B(A<N>& a) : a(a) {} void test() { af<1>(); // does not compile } }; int main() { A<2> a; af<1>(); // works fine B<2> b(a); b.test(); } 

g++ says:

 test2.cpp: In member function 'void B<N>::test()': test2.cpp:14: error: expected primary-expression before ')' token test2.cpp: In member function 'void B<N>::test() [with int N = 2]': test2.cpp:22: instantiated from here test2.cpp:14: error: invalid operands of types '<unresolved overloaded function type>' and 'int' to binary 'operator<' 

clang++ says:

 test2.cpp:14:16: error: expected expression af<1>(); // does not compile ^ 1 error generated. 

The same expression works in the context of main() , but not in the test() method of template class B , which has an instance of A as a private variable. I am pretty elusive why this is not working.

+3
c ++ templates compiler-errors
source share
1 answer

You should use a.template f<1>(); inside b.test() .

+5
source share

All Articles