提问者:小点点

我的C++类中产生的错误是什么? [已关闭]


我正在编写一个编译器,并在其中编写了一个由几个函数组成的C++类。 编译时,在函数定义string typeof(string name)的同一行中,它会产生两个错误,即在'name'之前出现[Error]预期的primary-expression和在'name'之前出现[Error]预期的')'。 而且就在这个函数定义的两行以上(无论它在哪里),在另一个函数的末尾,它会产生另一个错误,说[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;
        }
    }
    
};

共2个答案

匿名用户

我想你是被gcc编译器语言扩展给咬了。 请尝试使用typeof以外的名称,如type_oftypeof

您还可以使用-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.

匿名用户

使你的C++类更像C++。 您的代码因为“typeof”而没有编译--将其更改为typeof。 还将类名更改为以大写字母开头。 使用任何具有动态代码完成和检查的现代IDE--它会向您展示许多错误。

#include <string>

struct Element{
    std::string name;
    std::string type;
    std::string kind;
    int index;
    Element *nxt;
};

class SymbolTable{
private :
    Element *clas{nullptr};
    Element *sr{nullptr};
    Element *temp{nullptr};

    int st_varcount{};
    int fi_varcount{};
    int arg_varcount{};
    int loc_varcount{};
public :
    SymbolTable() :
        clas{nullptr},
        sr{nullptr},
        temp{nullptr},
        st_varcount{-1},
        fi_varcount{-1},
        arg_varcount{-1},
        loc_varcount{-1} { }

    void startSubroutine(){
        sr=nullptr;
        arg_varcount=-1;
        loc_varcount=-1;
    }

    void insert(Element *e, Element *head){
        if(head==nullptr){
            head=e;
        }
        else
        {
            temp=head;
            while(temp->nxt!=nullptr)
            {
                temp=temp->nxt;
            }
            temp->nxt=e;
        }
    }

    void define(std::string name, std::string type, std::string kind){
        Element *e;
        e=new Element;
        e->name=name;
        e->type=type;
        e->nxt=nullptr;
        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(std::string const &name){
        temp=sr;
        while(temp!=nullptr){
            if(temp->name.compare(name)==0){
                return temp;
            }
        }
        if(temp==nullptr){
            temp=clas;
            while(temp!=nullptr){
                if(temp->name.compare(name)==0) {
                    return temp;
                }
            }
            if(temp==nullptr){
                return nullptr;
            }
        }

        // return missing
        return nullptr;
    }

    std::string typeOf(std::string const &name) {
        auto l = search(name);
        if (l==nullptr) {
            return "NONE";
        }
        else
        {
            return l->type;
        }
    }

    std::string kindof(std::string const &name) {
        auto *l = search(name);
        if (l==nullptr) {
            return "NONE";
        }
        else{
            return l->kind;
        }
    }

    int indexof(std::string const &name) {
        Element *l{nullptr}; // a logic error here!!!
        if(l==nullptr){
            return std::numeric_limits<int>::max();
        }
        else{
            return l->index;
        }
    }
};

相关问题


MySQL Query : SELECT * FROM v9_ask_question WHERE 1=1 AND question regexp '(c++|类|中|关闭)' ORDER BY qid DESC LIMIT 20
MySQL Error : Got error 'repetition-operator operand invalid' from regexp
MySQL Errno : 1139
Message : Got error 'repetition-operator operand invalid' from regexp
Need Help?