Seb*_*ebi 0 c++ iterator vector std
我编写了一个包装器类,以对类型向量执行插入/删除操作。代码:
class GenericSymbolTable {
public:
virtual void pushBackAtom(Atom *atom) = 0;
virtual Atom* peekAtom(void) = 0;
virtual Atom* getAtom(void) = 0;
protected:
~GenericSymbolTable(void){}
};
class SymbolTable : public GenericSymbolTable {
private:
vector<Atom*> atoms;
protected:
~SymbolTable(void);
public:
void pushBackAtom(Atom *atom);
Atom* peekAtom(void);
Atom* getAtom(void);
};
Run Code Online (Sandbox Code Playgroud)
在为这些方法编写实现时,编译器会引发冲突的类型错误:
Atom* SymbolTable::peekAtom(void) {
if(atoms.empty()) {
cout << "\t[W] Simbol table does not contain any atoms" << endl;
return NULL;
}
Atom* first = atoms.begin(); // <== type error
return first;
}
Atom* SymbolTable::getAtom(void) {
if(atoms.empty()) {
cout << "\t[W] Simbol table does not contain any atoms" << endl;
return NULL;
}
Atom* first = atoms.begin(); // <== type error
atoms.erase(atoms.begin());
return first;
}
Run Code Online (Sandbox Code Playgroud)
错误消息:初始化时无法将'std :: vector :: iterator {aka __gnu_cxx :: __ normal_iterator>}'转换为'Atom *'
Atom* first = atoms.begin(); // <== type error
Run Code Online (Sandbox Code Playgroud)
设置为first
等于迭代器。您想将其设置为等于迭代器指向的对象。尝试:
Atom* first = *(atoms.begin());
Run Code Online (Sandbox Code Playgroud)
要么:
Atom* first = atoms.front();
Run Code Online (Sandbox Code Playgroud)
由于这是的向量Atom*
,因此其迭代器指向Atom*
s。