问题描述
我有以下 4 个文件:
I have the following 4 files:
arrayListType.h
:声明并定义arrayListType
类作为模板unorderedArrayListType.h
:继承自arrayListType
类并声明并定义unorderedArrayListType
作为模板.main1.cpp
:用于测试unorderedArrayListType
类的测试程序.Makefile
arrayListType.h
: Declare and definearrayListType
class as a templateunorderedArrayListType.h
: Inherited fromarrayListType
class and Declares and definesunorderedArrayListType
as a template.main1.cpp
: Test program to testunorderedArrayListType
class.Makefile
在unorderedArrayListType
中访问arrayListType
的受保护变量时出现编译错误,例如:长度未在此范围内声明"、列表未在此范围内声明"scope",其中长度和列表是 arrayListType
类中的受保护变量.
I get a compile error saying when accessing the protected variables of arrayListType
in unorderedArrayListType
for example: "length not declared in this scope", "list not declared in this scope", where length and list are protected variables in arrayListType
class.
以下是代码:
arrayListType.h
The following are the codes:
arrayListType.h
unorderedArrayListType.h
unorderedArrayListType.h
main1.cpp
生成文件:
以下是编译错误:
unorderedArrayListType
列出的更多函数和受保护的变量表示未在作用域中声明.想知道可能是什么错误.
More functions of unorderedArrayListType
listed and protected variables indicated as not declared in the scope. Wondering what could be the error.
新错误:
推荐答案
这是因为在首先检查模板的编译过程中未实例化模板类的模板父级.这些名称似乎不依赖于特定的模板实例,因此定义需要可用.(如果你从来不看arrayListType
的定义,那么阅读unorderedArrayListType
的代码就会出现list
和length
code> 需要是某种全局变量.)
This is because the template parent of a template class is not instantiated during the compilation pass that first examines the template. These names appear to be non-dependent on the particular template instantiation, and therefore the definitions need to be available. (If you never look at the definition of arrayListType
, then reading the code of unorderedArrayListType
it would appear the list
and length
need to be some sort of globals.)
您需要明确地告诉编译器这些名称实际上依赖于父项的实例化.
You'll need to tell the compiler explicitly that the names are in fact dependent on the instantiation of the parent.
一种方式,在所有继承的名字之前使用this->
:this->list
,this->length
.
One way, using this->
before all the inherited names: this->list
, this->length
.
另一种方式,使用声明:using arrayListType
等(例如在派生类的私有部分).
Another way, using declarations: using arrayListType<elemType>::length;
etc (for example in the private section of the derived class).
关于此的常见问题解答:https://isocpp.org/wiki/faq/templates#nondependent-name-lookup-members
A FAQ entry on this: https://isocpp.org/wiki/faq/templates#nondependent-name-lookup-members
这篇关于模板:父类成员变量在继承类中不可见的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!