c++ - 我的 c++ 类中产生的错误是什么?
问题描述
我正在编写一个编译器,并在其中编写了一个由几个函数组成的 c++ 类。在编译时,它会在函数定义的同一行中[Error] expected primary-expression before 'name'
产生两个错误 。而且就在这个函数定义的两行上方(无论它在哪里),在另一个函数的末尾,它会产生另一个错误,说. 还有一些更相似的函数定义(采用相同的参数并返回相同的数据类型)不会产生任何此类错误,而只会产生这个错误。我努力找出问题所在,但失败了。您能否验证以下课程的代码并就错误提供任何建议?先感谢您。[Error] expected ')' before 'name'
string typeof(string name)
[Error] expected unqualified-id at end of input
struct element{
string name, type, kind;
int index;
element *nxt;
};
class symbolTable{
private :
element *clas, *sr, *temp;
int st_varcount, fi_varcount, arg_varcount, loc_varcount;
public :
symbolTable(){
clas=NULL;
sr=NULL;
temp=NULL;
st_varcount=-1;
fi_varcount=-1;
arg_varcount=-1;
loc_varcount=-1;
}
void startSubroutine(){
sr=NULL;
arg_varcount=-1;
loc_varcount=-1;
}
void insert(element *e, element *head){
if(head==NULL){
head=e;
}
else{
temp=head;
while(temp->nxt!=NULL){
temp=temp->nxt;
}
temp->nxt=e;
}
}
void define(string name, string type, string kind){
element *e;
e=new element;
e->name=name;
e->type=type;
e->nxt=NULL;
if(kind.compare("field")==0){
e->kind="this";
}
else if(kind.compare("var")==0){
e->kind="local";
}
else if(kind.compare("arg")==0){
e->kind="argument";
}
else{
e->kind=kind;
}
if(e->kind.compare("static")==0){
st_varcount++;
e->index=st_varcount;
}
else if(e->kind.compare("this")==0){
fi_varcount++;
e->index=fi_varcount;
}
else if(e->kind.compare("argument")==0){
arg_varcount++;
e->index=arg_varcount;
}
else if(e->kind.compare("local")==0){
loc_varcount++;
e->index=loc_varcount;
}
if(e->kind.compare("static")==0 || e->kind.compare("this")==0){
insert(e, clas);
}
else if(e->kind.compare("argument")==0 || e->kind.compare("local")==0){
insert(e, sr);
}
}
element* search(string name){
temp=sr;
while(temp!=NULL){
if(temp->name.compare(name)==0){
return temp;
}
}
if(temp==NULL){
temp=clas;
while(temp!=NULL){
if(temp->name.compare(name)==0){
return temp;
}
}
if(temp==NULL){
return NULL;
}
}
}
string typeof(string name){
element *l;
l=search(name);
if(l==NULL){
return "NONE";
}
else{
return l->type;
}
}
string kindof(string name){
element *l;
l=search(name);
if(l==NULL){
return "NONE";
}
else{
return l->kind;
}
}
int indexof(string name){
element *l;
if(l==NULL){
return INT_MAX;
}
else{
return l->index;
}
}
};
解决方案
我认为您被gcc 编译器语言扩展所困扰。尝试使用 以外的名称typeof
,例如type_of
或typeOf
。
您还可以使用-fno-gnu-keywords
编译器选项:
-fno-gnu-keywords
Do not recognize typeof as a keyword, so that code can use this word as
an identifier. You can use the keyword __typeof__ instead. This option is
implied by the strict ISO C++ dialects: -ansi, -std=c++98, -std=c++11, etc.
推荐阅读
- javascript - 无需上传即可检索文件数据(Express、Multer)
- dom - Webapp按钮在谷歌表格中复制表格
- dynamics-crm - 模型驱动的 PowerApps
- ios - RealmSwiftObject 不由 Realm 管理。获取返回类的对象
- haskell - 在 Haskell 中,如何将一个类似列表的 monad 绑定到另一个类似列表的 monad
- typescript - 为什么 TypeScript 既有 `void` 又有 `undefined`?
- java - Webview 应用程序检查网络并在没有网络时显示“无互联网连接”,并在网络可用时重新加载
- javascript - 使用 JavaScript 设置的表单值验证失败
- c++ - 在循环中使用初始化列表是否有效?如果没有,它是如何失败的,为什么?
- python - Python嵌套字典更新任何嵌套键匹配的值