How to fix syntax in this code rich in patterns?

Following code

template<typename T, typename U> class Alpha { public: template<typename V> void foo() {} }; template<typename T, typename U> class Beta { public: Alpha<T, U> alpha; void arf(); }; template<typename T, typename U> void Beta<T, U>::arf() { alpha.foo<int>(); } int main() { Beta<int, float> beta; beta.arf(); return 0; } 

Compilation failed due to:

../src/main.cpp: in the member function 'Void Beta :: arf ():
../src/main.cpp:16: error: expected primary expression before 'int .. /src/main.cpp:16: error: expected "; before' int

How can i fix this? I tried everything I could think of.

+2
c ++ templates
source share
2 answers

alpha::foo is a dependent name , use alpha.template foo<int>() .

Dependent Names Assumed

  • not types unless typename prefix
  • should not be templates unless directly prefixed with template
+4
source share

Try alpha.template foo<int>() . Please note that your code compiles fine with VC8. Since alpha is a dependent type, you must indicate that foo is a template.

+4
source share

All Articles