C ++ expected primary expression error

I got the expected primary expression before the "int" error when compiling the following code with g ++. Do you know why and how to fix it? Thank!

 struct A
 {
     template <typename T>
     T bar() { T t; return t;}
 };

 struct B : A
 {
 };

 template <typename T>
 void foo(T  & t)
 {
     t.bar<int>();
 }

 int main()
 {
     B b;
     foo(b);
 }
+3
source share
1 answer

When compiling a function, the foo()compiler does not know that the panel is a member of the template . You must say that:

template <typename T>
void foo(T & t)
{
  t. template bar<int>(); // I hope I put template in the right position
}

The compiler considers that the bar is only a member variable, and you are trying to compare it with something, for example. t.bar < 10. As a result, he complains that "int" is not an expression.

+14
source

Source: https://habr.com/ru/post/1789580/


All Articles