2016-05-14 44 views
0

类:C++为什么我需要在扩展类中指定函数的作用域?

template <class TYPE, class KTYPE> 
class ExtAvlTree : public AvlTree<TYPE, KTYPE> { 
    public: 
     void ExtAvlTree_PrintDictionary(); 
     void ExtAvlTree_ProcessNode (KTYPE key); 
     void ExtAvlTree_insertNewWord(string, int); 
}; 

当我想用在AvlTree功能我不得不这样做:如果我不指定范围“AvlTree ::”我得到一个错误

template <class TYPE, class KTYPE> 
void ExtAvlTree<TYPE, KTYPE>::ExtAvlTree_insertNewWord(string word, int data) { 
    TreeNode newWord; 
    newWord.key = word; 
    newWord.data = data; 
    AvlTree<TYPE, KTYPE>::AVL_Insert(newWord); //look here <-- 
} 

error: there are no arguments to 'AVL_Insert' that depend on a template parameter, so a declaration of 'AVL_Insert' must be available [-fpermissive]| 

从我的知识,当在派生类中使用基类的功能时,我不必指定范围。如果有关系,我正在使用codeblocks 16.01 IDE。

+0

请发表[最小,完整,可验证的示例] (http://stackoverflow.com/help/mcve)。 –

+0

请注意,有问题的代码使用**模板**,因此“使用来自基类**类的函数”的分析可能会引起误解。模板不是类;它们是创建类的**模式**,它们有一套使模板实例化合理的规则,而这些规则不适用于类。 –

回答

1

From my knowledge when using functions from a base class in a derived class i dont have to specify the scope.

对于非模板类是如此,因为这样的查找是唯一定义的。在这种情况下,您正在使用模板类,因此AvlTree的查找不会唯一定义类型。实际上,AvlTree本身甚至不是一种类型,而是描述了可以使用不同模板参数创建的一组类型。

0

基类AvlTree不是非独立基类,并且AVL_Insert是非独立名称。非依赖名称不在相关的基类中查找。

要更正此代码,您需要取决于名称AVL_Insert,因为从名称只能在实例化时查找。那时候,必须探索的确切的基础专业化将是已知的。

当你显示,你可以

AvlTree<TYPE, KTYPE>::AVL_Insert(newWord); 

或使用this

this->AVL_Insert(newWord); 

或使用using

using AvlTree<TYPE, KTYPE>::AVL_Insert; 
AVL_Insert(newWord);