C++为什么我需要在扩展类中指定函数的作用域
C++ Why do I need to specify the scope of a function in an extended class?
类:
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中使用函数时,我必须这样做:
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 <--
}
如果我没有指定范围"AvlTree::",我会得到一个错误:
error: there are no arguments to 'AVL_Insert' that depend on a template parameter, so a declaration of 'AVL_Insert' must be available [-fpermissive]|
根据我的知识,当在派生类中使用基类中的函数时,我不必指定范围。如果重要的话,我使用的是代码块16.01 IDE。
根据我的知识,当在派生类中使用基类中的函数时,我不必指定范围。
对于非模板类来说,这是正确的,因为这样的查找是唯一定义的。在这种情况下,您使用的是模板类,因此AvlTree
的查找不会唯一定义类型。事实上,AvlTree
本身甚至不是一个类型,而是描述了一组可以使用不同模板参数创建的类型。
基类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);
相关文章:
- "变量":函数中函数作用域不允许初始化的自动或寄存器变量'naked'
- 当变量在多个函数作用域中使用时,我应该在类 private 中声明该变量吗?
- 缩短成员函数作用域说明符(嵌套类)(C++)
- 函数作用域是静态变量还是线程本地变量在C++11中的第一个条目中初始化
- 如何声明在函数作用域中定义的结构
- 我可以在函数作用域中使用定义指令吗?
- 为什么函数作用域不允许类前向声明?
- C++函数作用域中声明的返回引用
- 关于 C++(或 C)中的指针的函数作用域
- 如何在与 c++ 中定义的函数作用域相同的文件中限制函数作用域
- 函数作用域的静态非 Pod 对象初始化
- C++函数作用域
- 正在获取另一个函数作用域中的参数值
- 对临时对象的常量引用在函数作用域(生存期)后被破坏
- 被视为依赖作用域的函数模板的函数作用域结构体的静态成员函数
- 局部作用域和函数作用域的区别
- 函数作用域中的指针崩溃
- 将指针传递给函数只会改变函数作用域中的值
- 友元函数作用域和声明点
- using声明不能在函数作用域中重复.为什么