0

I have the following:

#include <vector>

template <class T>
class A
{
public:
    struct S
    {
        int a;
    };

    std::vector<S>  returnStructs(void);
};

template <class T>
std::vector<A<T>::S>    A<T>::returnStructs(void)
{
}

int main(void)
{
}

but when I try to compile I get:

error: template argument for template type parameter must be a type; did you forget
      'typename'?
std::vector<A<T>::S>    A<T>::returnStructs(void)
            ^
            typename

so I switched out that line for:

std::vector<A<int>::S>  A<T>::returnStructs(void)
              ^
              'int' instead of 'T'

but then I get a new compiler error:

error: return type of out-of-line definition of 'A::returnStructs' differs from that in the
      declaration
std::vector<A<int>::S>  A<T>::returnStructs(void)
~~~~~~~~~~~~~~~~~~~~~~        ^

so any thoughts on how to fix this?


Also I realize I can just take struct S out of class A and be done with all these issues but I still feel like it should be possible to solve this without changing class A.

Theo Walton
  • 1,085
  • 9
  • 24

1 Answers1

2

The first compiler error told you exactly what was wrong: did you forget 'typename'

As S is a member of a template you need to add typename to tell the compiler that it should delay the lookup of the name until after the template is resolved:

template <class T>
std::vector<typename A<T>::S>    A<T>::returnStructs(void)
Alan Birtles
  • 32,622
  • 4
  • 31
  • 60