問題描述
我有以下 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
#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
#endif
unorderedArrayListType.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
#endif
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;
}
生成文件:
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
In file included from main1.cpp:2:
unorderedArrayListType.h: In member function 'void unorderedArrayListType<elemType>::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
Undefined first referenced
symbol in file
arrayListType<int>::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
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
這篇關于模板:父類成員變量在繼承類中不可見的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!