joaocandre
joaocandre

Reputation: 1755

Correct syntax for separate definition of templated nested class methods

I'm writing a class that requires an additional nested class within, on top of being a template to allow different numeric types:

template<typename _type>
class myClass{
    // ...
    class myNestedClass{
        myNestedClass(int v1, int v2);
        myNestedClass& operator= (std::vector<int> _vals);
        operator std::vector<_type>() const;
        // ...
    }
    // ...
    template <typename _input_type> operator*= (_input_type _val);
    // ...
}

I've got most of the syntax down, particularly how to define the methods after class definition:

template <typename _type> 
template <typename _input_type>
myClass<_type>& myClass<_type>::operator*=(_input_type _val){ /* */ };

but I'm not being able to follow the same scheme for the nested class methods:

template <typename _type> 
myClass<_type>::myNestedClass::myNestedClass(int v1, int v2) { /* */ };

template <typename _type> 
myClass<_type>::myNestedClass& 
template <typename _type> myClass<_type>::myNestedClass::operator= (std::vector<int> _vals) { /* */ }

template <typename _type> 
myClass<_type>::myNestedClass::operator std::vector<_type> () const { /**/ };

But the compiler complains about the last two method definitions, with error: need 'typename' before 'myClass<_type>::myNestedClass' because 'myClass<_type>' is a dependent scope

So what exactly am I writing wrong here?

Upvotes: 1

Views: 45

Answers (1)

Acorn
Acorn

Reputation: 26194

See When is the "typename" keyword necessary?.

You were very close, anyway. After fixing it (plus a few typos around):

template<typename _type>
class myClass{
    // ...
    class myNestedClass{
        myNestedClass(int v1, int v2);
        myNestedClass& operator= (std::vector<int> _vals);
        operator std::vector<_type>() const;
        // ...
    };
    // ...
    template <typename _input_type> myClass<_type>& operator*= (_input_type _val);
    // ...
};

template <typename _type> 
template <typename _input_type>
myClass<_type>& myClass<_type>::operator*=(_input_type _val){ /* */ }

template <typename _type>
myClass<_type>::myNestedClass::myNestedClass(int v1, int v2) { /* */ }

template <typename _type> 
typename myClass<_type>::myNestedClass& myClass<_type>::myNestedClass::operator= (std::vector<int> _vals) { /* */ }

template <typename _type> 
myClass<_type>::myNestedClass::operator std::vector<_type> () const { /**/ }

Upvotes: 2

Related Questions