Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
C++ 这是我的代码中引发错误的部分:
IDlist->push_back(lex->getCurrentToken());
IDList 是一个向量,定义如下:
std::vector<Token*>* IDlist;
为什么那行代码不能推送我的 Token 对象?谢谢。
编辑:
当我尝试这个时:
Token* t = lex->getCurrentToken(); IDlist->push_back(t);
我犯了同样的错误; 当尝试推入向量时会发生这种情况。
IDlist 是一个指针,它指向一个向量,并且您没有通过new. 使用前需要先分配 IDlist:
new
IDlist = new std::vector<Token*>();
但是使用指向向量的指针有什么意义呢?只需将 IDlist 声明为变量:
std::vector<Token*> IDlist; Token* t = lex->getCurrentToken(); IDlist.push_back(t);