3

What is wrong with the following piece of code?

template<typename X>
struct A {
        template<int N>
        int foo() const {
                return N;
        }
};

template<typename X>
struct B {
        int bar(const A<X>& v) {
                return v.foo<13>();
        }
};

#include <iostream>
using std::cout;
using std::endl;

int main() {
        A<double> a;
        B<double> b;
        cout << b.bar(a) << endl;
        return 0;
}

Inside the function B::bar the compiler complains:

error: invalid operands of types ‘’ and ‘int’ to binary ‘operator<’

If A is not a template, everything compiles fine.

Prasoon Saurav
  • 91,295
  • 49
  • 239
  • 345
Danvil
  • 22,240
  • 19
  • 65
  • 88
  • 2
    possible duplicate of [c++ template syntax](http://stackoverflow.com/questions/3621719/c-template-syntax) – CB Bailey Sep 11 '10 at 14:54

1 Answers1

15

Change return v.foo<13>(); to return v.template foo<13>(); because foo is a dependent name and you need to mention that explicitly using .template construct.

Prasoon Saurav
  • 91,295
  • 49
  • 239
  • 345