gpt4 book ai didi

c++ - 相同的代码, vector 更改为 unordered_set 时出错

转载 作者:行者123 更新时间:2023-11-28 07:41:48 24 4
gpt4 key购买 nike

我有一个类:

class VectorAttrIterator : public AttrIterator {
vector<AttrValue>* values;
vector<AttrValue>::iterator it;
public:
VectorAttrIterator(vector<AttrValue>* _values) : values(_values) {
it = (*values).begin();
};

bool hasNext() {
return it != (*values).end();
};

AttrValue next() {
int ret = (*it);
it++;
return ret;
};

~VectorAttrIterator() {
delete values;
};
};

它有效。然后我想为 unordered_set 做一些类似的事情:

class UnorderedSetAttrIterator : public AttrIterator {
unordered_set<AttrValue>* values;
unordered_set<AttrValue>::iterator it;
public:
UnorderedSetAttrIterator(vector<AttrValue>* _values) : values(_values) {
it = (*values).begin();
};

bool hasNext() {
return it != (*values).end();
};

AttrValue next() {
int ret = (*it);
it++;
return ret;
};

~UnorderedSetAttrIterator() {
delete values;
};
};

唯一的变化是 vector 更改为 unordered_set 和类重命名。但我收到如下错误:

Error   42  error C2065: 'values' : undeclared identifier   h:\dropbox\sch\cs3202\code\source\includes\iterator.h   40
Error 43 error C2228: left of '.begin' must have class/struct/union h:\dropbox\sch\cs3202\code\source\includes\iterator.h 40
Error 44 error C2440: 'initializing' : cannot convert from 'std::vector<_Ty> *' to 'std::tr1::unordered_set<_Kty> *' h:\dropbox\sch\cs3202\code\source\includes\iterator.h 39
Error 45 error C2439: 'UnorderedSetAttrIterator::values' : member could not be initialized h:\dropbox\sch\cs3202\code\source\includes\iterator.h 39
Error 46 error C2065: 'it' : undeclared identifier h:\dropbox\sch\cs3202\code\source\includes\iterator.h 44
Error 47 error C2065: 'values' : undeclared identifier h:\dropbox\sch\cs3202\code\source\includes\iterator.h 44
Error 48 error C2228: left of '.end' must have class/struct/union h:\dropbox\sch\cs3202\code\source\includes\iterator.h 44
Error 49 error C2065: 'it' : undeclared identifier h:\dropbox\sch\cs3202\code\source\includes\iterator.h 48
Error 50 error C2065: 'it' : undeclared identifier h:\dropbox\sch\cs3202\code\source\includes\iterator.h 49
Error 51 error C2065: 'values' : undeclared identifier h:\dropbox\sch\cs3202\code\source\includes\iterator.h 54

怎么了?为什么未声明值? <强> Full Source

最佳答案

看起来这一行就是这个问题:

UnorderedSetAttrIterator(vector<AttrValue>* _values) : values(_values) {

您是否忘记将参数类型从 vector 更改为 unordered_set

关于c++ - 相同的代码, vector 更改为 unordered_set 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15704441/

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