我有以下4个文件: 1. arrayListType.h:声明并定义arrayListType类作为模板 2. unorderedArrayListType.h:继承自arrayListType类和Declares并定义unorderedArrayListType作为模板。 3. main1.cpp:测试程序以测试unorderedArrayListType类。 4. Makefile
I have the following 4 files: 1. arrayListType.h: Declare and define arrayListType class as a template 2. unorderedArrayListType.h: Inherited from arrayListType class and Declares and defines unorderedArrayListType as a template. 3. main1.cpp: Test program to test unorderedArrayListType class. 4. Makefile
我得到一个编译错误,当访问arrayListType的受保护的变量在unorderedArrayListType例如:length not declaration in this scope,list未声明在此范围,其中length和list是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
#ifndef H_arrayListType #define H_arrayListType #include <iostream> using namespace std; template <class elemType> class arrayListType { public: const arrayListType<elemType>&operator=(const arrayListType<elemType>&); bool isEmpty() const; bool isFull() const; int listSize() const; int maxListSize() const; void print() const; bool isItemAtEqual(int location, const elemType& item) const; virtual void insertAt(int location, const elemType& insertItem) = 0; virtual void insertEnd(const elemType& insertItem) = 0; void removeAt(int location); void retrieveAt(int location, elemType& retItem) const; virtual void replaceAt(int location, const elemType& repItem) = 0; void clearList(); virtual int seqSearch(const elemType& searchItem) const; virtual void remove(const elemType& removeItem) = 0; arrayListType(int size = 100); arrayListType(const arrayListType<elemType>& otherList); virtual ~arrayListType(); protected: elemType *list; int length; int maxSize; }; template <class elemType> bool arrayListType<elemType>::isEmpty() const { return (length == 0); } // remaining non-virtual functions of arrayListType class #endifunorderedArrayListType.h
unorderedArrayListType.h
#ifndef H_unorderedArrayListType #define H_unorderedArrayListType //#include <iostream> #include "arrayListType.h" //using namespace std; template <class elemType> class unorderedArrayListType: public arrayListType<elemType> { public: void insertAt(int location, const elemType& insertItem); void insertEnd(const elemType& insertItem); void replaceAt(int location, const elemType& repItem); int seqSearch(const elemType& searchItem) const; void remove(const elemType& removeItem); unorderedArrayListType(int size = 100); }; template <class elemType> void unorderedArrayListType<elemType>::insertAt(int location, const elemType& insertItem) { for(int i = length; i > location; i--) list[i] = list[i - 1]; list[location] = insertItem; length++; } // Remaining virtual functions that need to be defined by the inherited class #endifmain1.cpp
main1.cpp
#include <iostream> #include "unorderedArrayListType.h" using namespace std; int main() { unorderedArrayListType<int> intList(25); int number; cout<<"Line 3: Enter 8 integers: "; for(int count = 0; count < 8; count++) { cin>>number; intList.insertEnd(number); } cout<<"Line 8: intList: "; intList.print(); cout<<endl; }Makefile:
all: main1 main1.o: main1.cpp g++ -c -Wall main1.cpp main1: main1.o g++ -Wall main1.o -o main clean: rm -f *.o *~ main1以下是编译错误: make g ++ -c -Wall main1.cpp 在main1.cpp中包含的文件中:2: unorderedArrayListType.h:在成员函数'void unorderedArrayListType :: insertAt(int, const elemType&)': unorderedArrayListType.h:30:错误:'length'未在此范围中声明 unorderedArrayListType.h:31:错误:'list'在此范围中声明 unorderedArrayListType.h:33:错误:'list'未在此范围中声明
The following is the compilation error: make g++ -c -Wall main1.cpp In file included from main1.cpp:2: unorderedArrayListType.h: In member function 'void unorderedArrayListType::insertAt(int, const elemType&)': unorderedArrayListType.h:30: error: 'length' was not declared in this scope unorderedArrayListType.h:31: error: 'list' was not declared in this scope unorderedArrayListType.h:33: error: 'list' was not declared in this scope
列出和保护的unorderedArrayListType的更多函数变量表示为未在范围中声明。想知道什么可能是错误。
More functions of unorderedArrayListType listed and protected variables indicated as not declared in the scope. Wondering what could be the error.
新错误: make g ++ -Wall main1.o -o main 未定义首先引用 文件中的符号 arrayListType :: seqSearch(int const&)constmain1.o ld:fatal:引用符号引用错误。没有输出写入主 collect2:ld返回1退出状态 * 错误代码1 make:致命错误:命令失败的目标`main1'
New error: make g++ -Wall main1.o -o main Undefined first referenced symbol in file arrayListType::seqSearch(int const&) constmain1.o ld: fatal: Symbol referencing errors. No output written to main collect2: ld returned 1 exit status * Error code 1 make: Fatal error: Command failed for target `main1'
推荐答案这是因为模板类的模板父不在初始化检查模板。这些名称似乎不依赖于特定的模板实例化,因此定义需要可用。 (如果你从未看过 arrayListType 的定义,那么阅读 unorderedArrayListType 的代码就会出现 list 和 length 需要是某种全局变量。)
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- < code>, this->长度之前所有继承的名称 c>
One way, using this-> before all the inherited names: this->list, this->length.
另一种方式,使用声明:使用arrayListType< elemType> :: length; 在导出类的私有部分)。
Another way, using declarations: using arrayListType<elemType>::length; etc (for example in the private section of the derived class).
这是一个FAQ条目:www.parashift/c++-faq-lite/templates.html#faq-35.19
A FAQ entry on this: www.parashift/c++-faq-lite/templates.html#faq-35.19
更多推荐
templates:父类成员变量在继承类中不可见
发布评论