我编写了一个包装类来对类型向量执行插入/删除操作。编码:
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);
};
在为这些方法编写实现时,编译器会抛出冲突的类型错误:
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;
}
错误消息:无法在初始化中将 'std::vector::iterator {aka __gnu_cxx::__normal_iterator >}' 转换为 'Atom*'