2014-02-28 44 views
0

例如,我有以下类:如何重载模板的功能?

template<typename _Type> 
class MyClass 
{ 
    MyClass(); 
    const MyClass<_Type>& operator*(const MyClass<_Type> &inc); 
} 

template<typename _Type> 
const MyClass<_Type>& MyClass::operator*(const MyClass<_Type> &inc) // very much errors 
{ 
    //something 
} 

我怎么可以这样做呢?

回答

0

首先:由于任何类/结构也模板类必须声明后有terminatng ;

template<typename _Type> 
    class MyClass 
    { 
    // ... 
    }; 
//^
从您的样品

下一个错误,你忘了指定的模板类型:

template<typename _Type> 
const MyClass<_Type>& MyClass<_Type>::operator*(const MyClass<_Type> &inc) 
          // ^^^^^^^ 
{ 
    //something 
} 

另外你可能会注意到,这是模板类方法的简单定义,而不是overloadng

+0

OP也在类声明后忘记了分号。 'class MyClass {};'< - 那里。 – Brandon

+0

@CantChooseUsernames THX,很好的地方。我会提到这一点。 –