This answer applies to C++11 and earlier
There is no such thing as templated data members. The only sorts of templates are class template and function template. Your other example is a function template.
Try and think about how someone would instantiate your class. Suppose it was legal to do:
struct S
{
template<typename T>
T x;
};
int main()
{
S s;
s.x = ????
}
The type of S
must be known in order to do S s;
, it can't magically change to accommodate whatever type you decide to give to x
. In fact you couldn't even evaluate s.x
.
To solve your problem you have to make the template parameter be a template parameter of the class containing the data member, e.g.:
template<typename T>
struct S
{
vector<T> vec;
};
int main()
{
S<int> s;
s.vec = vector<int>(5);
}