Jiew Meng
Jiew Meng

Reputation: 88207

Same code, errors when vector is changed to unordered_set

I have a class like:

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;
    };
};

It works. Then I wanted to do something similar just for 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;
    };
};

The only changes are vector is changed to unordered_set and class renaming. But I get errors like:

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

Whats wrong? Why is values undeclared? Full Source

Upvotes: 0

Views: 156

Answers (1)

Daniel Frey
Daniel Frey

Reputation: 56863

It looks like this line is this problem:

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

Have you forgotten to change the parameter type from vector to unordered_set?

Upvotes: 1

Related Questions