0

The following code works:

template<typename T> class OtherClass
{
public:
    T member;
};

template<typename T> class MyClass
{         
public:
    vector<vector<OtherClass<T> > * > stacks;
};

template<typename T>
static vector<vector<OtherClass<T> > * > MyClass<T>::stacks =
    vector<vector<OtherClass<T> > * >(1024);

But this one does not:

template<typename T> class MyClass
{         
public:
    class OtherClass
    {
    public:
        T member;
    };

    static vector<vector<OtherClass> * > stacks;
};

template<typename T>
vector<vector<MyClass<T>::OtherClass> * > MyClass<T>::stacks =
    vector<vector<MyClass<T>::OtherClass> * >(1024);

gcc complains amongst other things: error: type/value mismatch at argument 1 in template parameter list for 'template class vector'

Any idea how to nest the class?

Thanks!

1 Answer 1

3

you need to tell the compiler that OtherClass is a nested type with typename

template<typename T>
vector<vector<typename MyClass<T>::OtherClass> * > MyClass<T>::stacks =
    vector<vector<MyClass<T>::OtherClass> * >(1024);

And no need to repeat the type twice :

template<typename T>
vector<vector<typename MyClass<T>::OtherClass> * > MyClass<T>::stacks (1024);

If you do not wants to repeat a complex original type, use typedef, still needing typename :

// in Myclass : using StackType = vector<vector<OtherClass>*>;
template<typename T>
typename MyClass<T>::StackType MyClass<T>::stacks (1024);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.