c++ - 我的 c++ 类中产生的错误是什么?

标签 c++ class oop compiler-errors syntax-error

关闭。这个问题是not reproducible or was caused by typos .它目前不接受答案。












想改进这个问题?将问题更新为 on-topic对于堆栈溢出。

1年前关闭。




Improve this question




我正在编写一个编译器,并在其中编写了一个由几个函数组成的 c++ 类。在编译时,它会产生两个错误,说 [Error] expected primary-expression before 'name'[Error] expected ')' before 'name'在函数定义的同一行 string typeof(string name) .而且就在这个函数定义的两行上方(无论它在哪里),在另一个函数的末尾,它会产生另一个错误,说 [Error] expected unqualified-id at end of input .还有一些更相似的函数定义(采用相同的参数并返回相同的数据类型)不会产生任何此类错误,而只会产生这个错误。我努力找出问题所在,但失败了。您能否验证以下类(class)的代码并就错误提供任何建议?先感谢您。

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 compiler language extension .尝试使用 typeof 以外的名称,如 type_oftypeOf .
您也可以使用 -fno-gnu-keywords compiler option :

-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.

关于c++ - 我的 c++ 类中产生的错误是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63231440/

相关文章:

javascript - 延迟一段时间后更改 div 的类

Python:按顺序执行步骤

java - 进行 OOP 时,无法在 if else 中输出字符串

C++ - 如何让移动点连续循环?

c++ - 如何在模板类的 'namespace' 中创建非模板类型定义?

c++ - 在 C++11 中传递具有未指定参数的函数?

java - Android - SignalStrength 是事件类吗?

c++ - 通过类函数返回引用并在 C++ 中返回整个对象?

c++ - 性能测量显示每个函数调用的 malloc 影响?

c++ - 析构函数中函数调用的段错误