Project2.exe中0x74A635D2的未处理异常:Microsoft C ++异常:内存位置0x012FDBC4的std :: bad_alloc

时间:2019-12-10 19:30:54

标签: c++ pointers

我创建了一个名为“ Consulta Bool”的类,该类应该创建树状结构。我使用此类来进行布尔咨询。这就是为什么我创建了一个类(我不得不使用头文件和.cc文件)的原因,这是头文件和.cc文件的代码:


struct token {
    string tipo; 
    string etq; 
    Date fini, ffin; 
};
//(there is an endif here)  

class ConsultaBool {
public:
    ConsultaBool(const string& s);
    ConsultaBool();
    ConsultaBool(const Date& fini, const Date& ffin); 
    ConsultaBool AND(ConsultaBool q2); 
    ConsultaBool OR(ConsultaBool q2);  
    ConsultaBool NOT();                       
    void read(istream& is);
    ConsultaBool getEsquerra();
    ConsultaBool getDreta();
    token getToken();

private: 
    token arrel;
    ConsultaBool* dreta; //dreta mins right side of the Bin Tree
    ConsultaBool* esquerra; //esquerra means left side 
};

(there is another endif here)'''```

and here comes the heavy stuff;


string toUpper(const string& s) {
    string res = "";
    for (int i = 0; i < s.length(); ++i)
        res += toupper(s[i]);
    return res;
}

void read_input(istream& is, vector<token>& l);
int precedence(string oper);
void convert_infix_to_postfix(const vector<token>& infix, vector<token>& postfix);
ConsultaBool construct_bool_query(const vector<token>& postfix);

ConsultaBool::ConsultaBool(const string& s) {
    this -> arrel.etq = s;
    this -> arrel.tipo = "TAG";
}

ConsultaBool::ConsultaBool(const Fecha& fini, const Fecha& ffin) {
    this ->arrel.fini = fini;
    this ->arrel.ffin = fini;
    this ->arrel.tipo = "DATE";
} 

ConsultaBool::ConsultaBool() {
    this->arrel.tipo = "#";
} 

token ConsultaBool::getToken() {
    return arrel;
}

ConsultaBool ConsultaBool::getDreta() {
    return *dreta;
}

ConsultaBool ConsultaBool::getEsquerra() {
    return *esquerra;
}
ConsultaBool ConsultaBool::AND(ConsultaBool q2) {
    ConsultaBool consulta;
    consulta.arrel.tipo = "AND";
    consulta.dreta = this;
    consulta.esquerra = &q2;
    return consulta;
} 

ConsultaBool ConsultaBool::OR(ConsultaBool q2) {
    ConsultaBool consulta;
    consulta.arrel.tipo = "OR";
    consulta.dreta = this;
    consulta.esquerra = &q2;
    return consulta;
}  

ConsultaBool ConsultaBool::NOT() {
    ConsultaBool consulta;
    consulta.arrel.tipo = "NOT";
    consulta.esquerra = this;
    return consulta;
}                       

ConsultaBool construct_bool_query(const vector<token>& postfix) {
    stack<ConsultaBool> S;
    for (int i = 0; i < postfix.size(); ++i) {
        token tk = postfix[i];
        if (tk.tipo == "TAG") S.push(ConsultaBool(tk.etq)); 
        if (tk.tipo == "DATE") S.push(ConsultaBool(tk.fini, tk.ffin)); 
        if (tk.tipo == "AND" or tk.tipo == "OR") {
            ConsultaBool q2 = S.top(); S.pop();
            ConsultaBool q1 = S.top(); S.pop();
            if (tk.tipo == "AND") S.push(q1.AND(q2)); 
            if (tk.tipo == "OR") S.push(q1.OR(q2)); 
        }
        if (tk.tipo == "NOT") {
            ConsultaBool q = S.top(); S.pop(); 
            S.push(q.NOT()); // apila una consulta 'not q'
        }
    }
    return S.top();
}

ConsultaBool::read(istream& is) {

    vector<token> infix, postfix;
    read_input(is, infix);
    if (infix.empty()) { 
        *this = ConsultaBool(); 
        return; 
    }
    convert_infix_to_postfix(infix, postfix);
    *this = construct_bool_query(postfix);
}

void read_input(istream& is, vector<token>& l) {
    string s; 
    is >> s; 
    while (s != "END_QUERY") {
        token tk;
        s = toUpper(s);
        tk.tipo = s;
        if (s == "(" or s == ")" or s == "AND" or s == "OR" or s == "NOT") { 
            l.push_back(tk);
        }
        if (s == "DATE") { /** usad el metodo Fecha::llegir() o similar si no teneis
                            sobrecargado el operador >> para leer fechas **/
            tk.fini.lee_fecha();
            tk.ffin.lee_fecha();
            l.push_back(tk);
        }
        if (s == "TAG") {
            is >> tk.etq;
            l.push_back(tk);
        }      
        is >> s;
    }
}

/**THEORETICALLY FROM THIS POINT ON EVERYTHING SHOULD BE CORRECT **/

int precedence(string oper) {
    if (oper == "OR") return 1;
    if (oper == "AND") return 2;
    if (oper == "NOT") return 3;
    return 0;
}

void convert_infix_to_postfix(const vector<token>& infix, vector<token>& postfix) {
    stack<token> S;
    token spc; spc.tipo = "#"; S.push(spc);
    for (int i = 0; i < infix.size(); ++i) {
        token tk = infix[i];
        if (tk.tipo == "DATE" or tk.tipo == "TAG")
            postfix.push_back(tk);
        else if (tk.tipo == "(" or tk.tipo == "NOT") 
            S.push(tk);
        else if (tk.tipo == ")") {
            while (S.top().tipo != "#" and S.top().tipo != "(") {
                postfix.push_back(S.top()); S.pop();
            }
            S.pop();
        } else {
            while (S.top().tipo != "#" and 
                precedence(tk.tipo) <= precedence(S.top().tipo)) {
                    postfix.push_back(S.top()); S.pop();
            }
            S.push(tk);
        }
    }
    while (S.top().tipo != "#") {
        postfix.push_back(S.top()); S.pop();
    } 
}

很抱歉,仅粘贴代码。访问函数“ getEsquerra()”(这只是一个吸气剂)时遇到问题。它告诉我Project2.exe中0x74A635D2处有未处理的异常:Microsoft C ++异常:内存位置0x012FDBC4处的std :: bad_alloc。知道为什么会发生这种情况吗?

0 个答案:

没有答案