C++ 抽象基类具有抽象嵌套类
我的代码结构如下。 我有一个基础抽象类,它也有一个嵌套的抽象迭代器。我从基类继承,也从抽象基类继承。要分配对象,我使用多态性: 我遇到了如下错误:C++ 抽象基类具有抽象嵌套类,c++,iterator,abstract-class,nested-class,C++,Iterator,Abstract Class,Nested Class,我的代码结构如下。 我有一个基础抽象类,它也有一个嵌套的抽象迭代器。我从基类继承,也从抽象基类继承。要分配对象,我使用多态性: 我遇到了如下错误: In file included from newDataStore.h:6:0, from newDataStore.cpp:1: ../HashTable/baseHashTable.h: In instantiation of ‘class BaseHashTable<std::b
In file included from newDataStore.h:6:0,
from newDataStore.cpp:1:
../HashTable/baseHashTable.h: In instantiation of ‘class BaseHashTable<std::basic_string<char>, User*>::iterator’:
../HashTable/hashTable.h:53:8: required from ‘class HashTable<std::basic_string<char>, User*>::iterator’
../HashTable/hashTable.h:8:7: required from ‘class HashTable<std::basic_string<char>, User*>’
newDataStore.cpp:10:25: required from here
../HashTable/baseHashTable.h:59:19: error: cannot allocate an object of abstract type ‘BaseHashTable<std::basic_string<char>, User*>::iterator’
virtual iterator begin() const = 0;
^
../HashTable/baseHashTable.h:27:8: note: because the following virtual functions are pure within ‘BaseHashTable<std::basic_string<char>, User*>::iterator’:
class iterator
在newDataStore.h:6:0中包含的文件中,
来自newDataStore.cpp:1:
../HashTable/baseHashTable.h:在“类baseHashTable::迭代器”的实例化中:
../HashTable/HashTable.h:53:8:必须来自“类HashTable::迭代器”
../HashTable/HashTable.h:8:7:在“类哈希表”中是必需的
newDataStore.cpp:10:25:此处为必填项
../HashTable/baseHashTable.h:59:19:错误:无法分配抽象类型为“baseHashTable::iterator”的对象
虚拟迭代器begin()常量=0;
^
../HashTable/baseHashTable.h:27:8:注意:因为以下虚拟函数在“baseHashTable::iterator”中是纯函数:
类迭代器
我的代码结构合适吗?如何消除编译器错误:无法分配抽象类的对象
Code:
// Instantiating the hash table
myUserHashTable = new HashTable<string, User*>;
template<class KeyType, class ValueType>
class BaseHashTable // abstract class
{
class iterator
{
public:
virtual const ValueType& operator*() const = 0;
virtual iterator operator++() = 0;
bool operator==(const iterator& other) const
{
return (row == other.row && parent_ == other._parent);
}
bool operator!=(const iterator& other) const
{
return !(*this == other);
}
friend class BaseHashTable;
virtual BaseHashTable<KeyType,ValueType>* getParent() const = 0;
protected:
iterator(int row, const BaseHashTable* parent)
{
this->row = row;
parent_ = parent;
}
int row;
const BaseHashTable* parent_;
};
virtual iterator begin() const = 0;
virtual iterator end() const = 0;
protected:
int _size;
};
template<class KeyType, class ValueType>
class HashTable : public BaseHashTable<KeyType, ValueType>
{
class iterator: public BaseHashTable<KeyType, ValueType>::iterator
{
public:
const ValueType& operator*() const
{
return getParent()->hashTB[this->row]->at(col).second;
}
iterator operator++()
{
if (getParent()->hashTB[this->row]->size() > col+1)
{
col += 1;
return *this;
} else {
for (int i = this->row+1; i < this->parent_->_size; ++i)
{
if(getParent()->hashTB[i]->size() > 0)
{
this->row = i;
col = 0;
return *this;
}
}
this->row = getParent()->_size;
col = 0;
return *this;
}
}
bool operator==(const iterator& other) const
{
return (this->col == other.col) ? BaseHashTable<KeyType, ValueType>::iterator::operator==(other) : false;
}
bool operator!=(const iterator& other) const
{
return !(*this == other);
}
HashTable<KeyType,ValueType>* getParent() const
{
return ((HashTable<KeyType, ValueType>*)this->parent_);
}
friend class HashTable<KeyType, ValueType>;
private:
iterator(int row, int col, const BaseHashTable<KeyType, ValueType>* parent): BaseHashTable<KeyType, ValueType>::iterator(row, parent)
{
this->col = col;
}
int col;
};
iterator begin() const
{
typename std::vector<std::pair<KeyType, ValueType> >::iterator it;
int j = 0;
for (int i = 0; i < this->_size; ++i)
{
if(hashTB[i]->size() > 0)
return iterator(j,0, this);
j++;
}
}
iterator end() const {
return iterator(this->_size, 0, this);
}
protected:
std::vector<std::pair<KeyType, ValueType> >** hashTB;
};
template<class KeyType, class ValueType>
class DoubleHashingHashTable : public BaseHashTable<KeyType, ValueType>
{
class iterator {/*Implementation*/ } : public BaseHashTable<KeyType, ValueType>::iterator
iterator begin() const {/*Implementation*/}
iterator end() const {/*Implementation*/}
};
代码:
//实例化哈希表
myUserHashTable=新的HashTable;
模板
类BaseHashTable//抽象类
{
类迭代器
{
公众:
虚拟常量ValueType&运算符*()常量=0;
虚拟迭代器运算符++()=0;
布尔运算符==(常量迭代器和其他)常量
{
返回(row==other.row&&parent==other.\u parent);
}
布尔运算符!=(常量迭代器和其他)常量
{
返回!(*此==其他);
}
好友类BaseHashTable;
虚拟BaseHashTable*getParent()常量=0;
受保护的:
迭代器(int行,const BaseHashTable*parent)
{
此->行=行;
父母=父母;
}
int行;
常量BaseHashTable*父项;
};
虚拟迭代器begin()常量=0;
虚拟迭代器end()常量=0;
受保护的:
内部尺寸;
};
模板
类哈希表:公共BaseHashTable
{
类迭代器:publicbasehashtable::迭代器
{
公众:
常量值类型和运算符*()常量
{
返回getParent()->hashTB[this->row]->at(col).second;
}
迭代器运算符++()
{
if(getParent()->hashTB[this->row]->size()>col+1)
{
col+=1;
归还*这个;
}否则{
对于(inti=this->row+1;iparent->\u size;++i)
{
if(getParent()->hashTB[i]->size()>0)
{
这->行=i;
col=0;
归还*这个;
}
}
此->行=getParent()->\u大小;
col=0;
归还*这个;
}
}
布尔运算符==(常量迭代器和其他)常量
{
return(this->col==other.col)?BaseHashTable::iterator::operator==(other):false;
}
布尔运算符!=(常量迭代器和其他)常量
{
返回!(*此==其他);
}
哈希表*getParent()常量
{
返回((HashTable*)this->parent;
}
朋友类哈希表;
私人:
迭代器(int row,int col,const BaseHashTable*parent):BaseHashTable::迭代器(row,parent)
{
这个->列=列;
}
int col;
};
迭代器begin()常量
{
typename std::vector::iterator;
int j=0;
对于(int i=0;i\u size;++i)
{
if(hashTB[i]->size()>0)
返回迭代器(j,0,this);
j++;
}
}
迭代器end()常量{
返回迭代器(这个->\u大小,0,这个);
}
受保护的:
std::vector**hashTB;
};
模板
类DoubleHashingHashTable:公共BaseHashTable
{
类迭代器{/*实现*/}:公共BaseHashTable::迭代器
迭代器begin()常量{/*实现*/}
迭代器end()常量{/*实现*/}
};
迭代器不能是抽象类,甚至不能是多态类。通过值传递或返回需要创建迭代器类的新的完整对象。派生类的任何属性都将通过“切片”操作剥离
您可能需要类型擦除,即非多态对象通过拥有某个多态对象来获得多态行为。不过,实现这一点的简单方法涉及堆,通常迭代器(无论如何,容器迭代器)可以避免堆分配的复杂性。在子类中定义
begin
和end
成员函数,但是他们没有返回任何东西?你的问题是什么?我已经实现了这些函数,但没有在这里编写实现。展示一个例子有点困难,因为我的项目有30多个文件。我的问题是:代码结构合适吗?其次,我的类中甚至没有使用迭代器,那么为什么会出现编译器错误,比如:无法分配抽象类的对象缺少几个分号。您知道如何终止类声明吗?谢谢!所以基本上我必须编写所有迭代器类,而不需要任何继承?@saurabhjainpriva
virtual iterator begin() const = 0;