我有一个存储一些符号的类。
class SymbolInfo{
public:
string name;
string type;
string token;
int num;
char ch;
float fl;
int arrSize;
int array_index_holder;
SymbolInfo *next;
Function *func;
SymbolInfo* myarray;
SymbolInfo(){
name="";
type="";
token="";
next = NULL;
arrSize = -1;
array_index_holder = -1;
}
SymbolInfo(string name, string type){
this->name = name;
this->type = type;
token="";
next = NULL;
arrSize = -1;
array_index_holder = -1;
}
string getname(){
return name;
}
void setname(string name){
name = name;
}
string gettype(){
return type;
}
void settype(string type){
type = type;
}
};
我想制作一个符号数组。所以我在 Vector
中使用了这个类并创建了这个:
Vector<SymbolInfo*>array;
对于整数大小,我试图创建一个这样的数组:
SymbolInfo *s= table->LookUp($3->name);
$3->type = dataType;
table->Insert($3->name, $3->type);
s = table->LookUp($3->name);
s->token = "ARRAY";
int size = $5->num;
for(int i=0;i<size;i++){
array[i].push_back(*s);
}
$3 和 $5 是 SymbolInfo 类型的指针(用于 YACC/BISON) 执行此操作后出现此错误:
parser.y: In function ‘int yyparse()’:
parser.y:200:12: error: request for member ‘push_back’ in ‘array.std::vector<_Tp, _Alloc>::operator[]<SymbolInfo*, std::allocator<SymbolInfo*> >(((std::vector<SymbolInfo*>::size_type)i))’, which is of pointer type ‘SymbolInfo*’ (maybe you meant to use ‘->’ ?)
array[i].push_back(*s);
^
我做错了什么?如何解决这个问题?
最佳答案
如果 array 是一个 vector 而不是仅仅调用它的 push_back。它会将元素放在 vector 的后面。如果您使用 [] 运算符,它会计算为对 vector 中项目的引用,在这种情况下是指向某些符号表信息的指针。如果你想在索引 i 的 vector 中存储一些东西,给定 vector 有一个索引 i 然后使用代码行
vector[i] = something;
关于c++ - 如何在 Vector 中存储类指针?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43806079/