抽象基类具有抽象的嵌套类
Abstract base class has abstract nested class
我的代码结构如下。我有一个基本的抽象类,它还具有嵌套的抽象迭代器。我从基类继承,也从抽象基础继承类继承。为了分配我使用多态性的对象:
我遇到的错误:
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
我的代码结构合适吗?如何摆脱编译器错误:不能分配抽象类的对象?
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*/}
};
virtual iterator begin() const = 0;
迭代器不能是抽象类,甚至是多态类别。通过值传递或返回需要创建迭代器类的新的,完整的对象。派生类的任何属性将被"切片"操作剥离。
您可能想要 type Erasure ,其中非晶状体对象通过拥有多态性的东西来获得多态性行为。但是,这样做的简单方法涉及堆,通常是迭代器(无论如何,容器迭代器)避免了堆分配的复杂性。
相关文章:
- 在nlohmann json中,如何将嵌套对象的数组转换为嵌套结构的向量
- C++-模板嵌套类的引用初始化无效
- 无法掌握嵌套循环的写作技巧
- 在 c++ 中实现嵌套循环的更短方法吗?
- 2 个嵌套循环的时间复杂度
- C++中具有嵌套条件的 X 模式
- 在嵌套 lambda 的情况下如何初始化 lambda 捕获?
- 如何在该接收类之外声明嵌套类的方法
- 如何从另一个嵌套类中调用某个封闭类的嵌套类的函数指针成员的值?
- 瓦尔格林德报告在 =带有嵌套shared_ptrs的运算符上的读取错误
- 如何访问嵌套类的数据?
- 另一个类中嵌套模板的外部实例化
- 没有从Singleton到嵌套Listener*的可行转换
- 在模板类内部定义的枚举上的嵌套类的部分专用化
- 两个嵌套循环的运行时间复杂性:二次型还是线性
- 什么是"递归嵌套模板"的实际用法示例?
- 带有 if 语句的嵌套循环的时间复杂度
- 嵌套抽象基类的泛型模板
- C 带有嵌套类的抽象类.派生的类和嵌套类
- c++派生出带有嵌套结构的纯抽象