相同的代码,向量更改为unordered_set时的错误

时间:2013-03-29 13:39:58

标签: c++ visual-c++ vector compiler-errors set

我有一个类:

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

1 个答案:

答案 0 :(得分:1)

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

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

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