2013-03-29 14 views
0

我有這樣一個類:相同的代碼,錯誤時,矢量改爲unordered_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

+2

你有'#include '以及'#include '嗎? –

+0

@AndyProwl這是如何解決C++的問題:'#include using namespace magic;':P – 2013-03-29 13:43:45

+0

@ H2CO3:lol,你真的比我應得的讚美更多;) –

回答

1

它看起來像這條線是這個問題:

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

你忘了從vector參數類型更改爲unordered_set