gpt4 book ai didi

我类的 C++ 迭代器没有将我的结构作为一种类型

转载 作者:行者123 更新时间:2023-11-28 02:29:16 25 4
gpt4 key购买 nike

我有一个名为 MyHashMap 的类,我想实现一个迭代器,但由于某些原因我不能。我有一个 vector ,它接受一个私有(private)结构作为类型就好了,但是当我尝试在我的公共(public)部门中用迭代器定义它时,它说 HashEntry 没有声明。这是我的类(class)。

template<typename KeyType, typename ObjectType>
class MyHashMap
{
public:

/***********ITERATOR FUNCTIONS****************/

typedef typename std::vector<HashEntry>::iterator iterator;
typedef typename std::vector<HashEntry>::const_iterator const_iterator;

iterator begin() { return array.begin(); }

const_iterator begin() const { return array.begin(); }

iterator end() { return array.end(); }

const_iterator end() const { return array.end(); }

private:
struct HashEntry
{
KeyType element;
ObjectType mapped;
EntryType info;

HashEntry( const KeyType & e = KeyType{ },
const ObjectType & m = ObjectType{ },
EntryType i = EMPTY )
: element{ e }, mapped{ m }, info{ i } { }

HashEntry( KeyType && e,
ObjectType && m,
EntryType i = EMPTY )
: element{ std::move( e ) }, mapped{ std::move( m ) }, info{ i } { }
};

vector<HashEntry> array;
};

我把这些当作错误

error: ‘HashEntry’ was not declared in this scope
typedef typename std::vector<HashEntry>::iterator iterator;
^
error: template argument 1 is invalid
typedef typename std::vector<HashEntry>::iterator iterator;
^
error: template argument 2 is invalid
error: ‘HashEntry’ was not declared in this scope
typedef typename std::vector<HashEntry>::const_iterator const_iterator;
^
error: template argument 1 is invalid
typedef typename std::vector<HashEntry>::const_iterator const_iterator;
^
error: template argument 2 is invalid

有什么问题吗?我很确定答案很简单,但我就是想不通。先感谢您。

最佳答案

那是因为您声明HashEntry 之后您将它用作模板参数。

应该是这样的:

template<typename KeyType, typename ObjectType>
class MyHashMap
{
private:
struct HashEntry
{
KeyType element;
ObjectType mapped;
EntryType info;

HashEntry( const KeyType & e = KeyType{ },
const ObjectType & m = ObjectType{ },
EntryType i = EMPTY )
: element{ e }, mapped{ m }, info{ i } { }

HashEntry( KeyType && e,
ObjectType && m,
EntryType i = EMPTY )
: element{ std::move( e ) }, mapped{ std::move( m ) }, info{ i } { }
};

vector<HashEntry> array;

public:
/***********ITERATOR FUNCTIONS****************/

//Now you can use 'HashEntry' as it is visible now:

typedef typename std::vector<HashEntry>::iterator iterator;
typedef typename std::vector<HashEntry>::const_iterator const_iterator;

iterator begin() { return array.begin(); }

const_iterator begin() const { return array.begin(); }

iterator end() { return array.end(); }

const_iterator end() const { return array.end(); }
};

编辑

我认为前向声明在这里应该足够了,因为当 MyHashMap 被实例化时, HashEntry 已经被定义了。

但我认为,您应该坚持在类(尤其是模板类)中声明事物的一致顺序。我总是这样做:

例如,这是我的自定义容器之一的片段:- 内部类型和结构- 基础迭代器- 公共(public)类型定义- 成员- 公共(public)接口(interface)然后,一切总是在正确的地方。

template <class T>
class SortedArray
{
protected:
class Block
{
//...
};

class InternalCompare
{
//...
};

template <IteratorType Iter_type>
class IteratorBase
{
//...
};

template <IteratorType Iter_type>
class InternalIterator : public IteratorBase<Iter_type>
{
//...
};

template <IteratorType Iter_type>
class InternalReverseIterator : public IteratorBase<Iter_type>
{
//...
};

public:
typedef SortedArray<T> MyType;

typedef InternalIterator<IteratorType::Non_Const> Iterator;
typedef InternalIterator<IteratorType::Const> ConstIterator;
typedef InternalReverseIterator<IteratorType::Non_Const> ReverseIterator;
typedef InternalReverseIterator<IteratorType::Const> ConstReverseIterator;


protected:
DynamicBuffer<Block> _blocks;
Size_t _blocks_num;
Size_t _elements_num;
Block* _first_block;
Block* _last_block;

public:
SortedArray()
{
//...
};

//etc.
};

但这只是一个例子。

编辑 2

已发布解决方案的另一种解决方案是仅向前声明 HashEntry。这只会修改您的原始代码,增加一行代码:

//This is your original code.
template<typename KeyType, typename ObjectType>
class MyHashMap
{
private:
struct HashEntry; //This forward declaration is sufficient for everything to work properly.

public:
/***********ITERATOR FUNCTIONS****************/
typedef typename std::vector<HashEntry>::iterator iterator;
typedef typename std::vector<HashEntry>::const_iterator const_iterator;

iterator begin() { return array.begin(); }

const_iterator begin() const { return array.begin(); }

iterator end() { return array.end(); }

const_iterator end() const { return array.end(); }

private:
struct HashEntry
{
KeyType element;
ObjectType mapped;
EntryType info;

HashEntry( const KeyType & e = KeyType{ },
const ObjectType & m = ObjectType{ },
EntryType i = EMPTY )
: element{ e }, mapped{ m }, info{ i } { }

HashEntry( KeyType && e,
ObjectType && m,
EntryType i = EMPTY )
: element{ std::move( e ) }, mapped{ std::move( m ) }, info{ i } { }
};

vector<HashEntry> array;
};

关于我类的 C++ 迭代器没有将我的结构作为一种类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29448599/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com