0

I was experimenting with C++ templates today and got stuck on this piece of code.

template <typename T>
class XYZ
 {
  public:
    T t;
 };

template <typename A>
class Foo
 {
  public:
   template <typename C>
   class Bar
    {
     public:
      C a;
    };

  Bar < A > fx ();
  XYZ < Bar <A> > gx ();
 };

template <typename A>
Foo<A>::Bar <A> Foo <A> :: fx ()
 {
  return Bar <A> ();
 }

template <typename A>
XYZ < Foo <A> :: Bar <A> > Foo <A> :: gx ()
 {
  XYZ < Foo <A> :: Bar <A> > xyz;
  xyz.t = Foo <A> :: Bar <A> ();    
  return xyz;
 }

int main ()
 {
  Foo <int> foo;
  foo.fx();
  foo.gx();
 }

The compiler says in the function declaration of gx(), there is a type mismatch for A in Bar <A> of the return type XYZ < Foo <A> :: Bar <A> > and I have absolutely no idea why for the past three hours.
Here is the exact compiler message if it helps.

test.cpp:30:24: error: type/value mismatch at argument 1 in template parameter list for 'template<class T> class XYZ'
 XYZ < Foo <A> :: Bar <A> > Foo <A> :: gx ()
                        ^
test.cpp:30:24: note:   expected a type, got '(Foo<A>::Bar < <expression error>)'
test.cpp:30:26: error: expected unqualified-id before '>' token
 XYZ < Foo <A> :: Bar <A> > Foo <A> :: gx ()

0 Answers0