简体   繁体   English

正确的语法,用于模板化嵌套类方法的单独定义

[英]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 但是编译器抱怨最后两个方法定义,但有error: need 'typename' before 'myClass<_type>::myNestedClass' because 'myClass<_type>' is a dependent scope

So what exactly am I writing wrong here? 那么我在这里写错了什么呢?

See When is the "typename" keyword necessary? 请参阅何时需要“ typename”关键字? .

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 { /**/ }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM