C++ “CL.exe” завершилась с кодом 2 [закрыт]

69
06 апреля 2021, 03:10
Закрыт. Этот вопрос не по теме. Ответы на него в данный момент не принимаются.

Хотите улучшить этот вопрос? Обновите вопрос так, чтобы он вписывался в тематику Stack Overflow на русском.

Закрыт 1 год назад.

Улучшить вопрос

Не как не могу понять и делать с ошибкой "CL.exe" завершилась с кодом 2. Даже не знаю, о чем она говорит. Ошибка пропадает если убрать строчку в int main(); a -= g; . Как исправить так и не понял. (Извиняюсь за большой код)

#include <iostream>
#include <vector>
#include <string>
#include <functional>
#include <algorithm>
////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////
class Monomial {
public:
    void get_num(std::string::iterator& it, int& num, const std::string& str) const {
        while (it != str.end() && '0' <= *it && *it <= '9') {
            num += *it - '0';
            num *= 10;
            ++it;
        }
        num /= 10;
    }
    Monomial(int x) : coef(x), deegs(26) {}
    Monomial(std::string str = "") : deegs(26) { // Example input: +21x71z1
        if (str.size() == 0) {
            coef = 0;
            return;
        }
        std::string::iterator it = str.begin();
        coef = 0;
        if (*it == '-') {
            ++it;
            get_num(it, coef, str);
            coef *= -1;
        } else {
            ++it;
            get_num(it, coef, str);
        }
        int curr_x = 0;
        while (it != str.end()) {
            curr_x = *it - 'a';
            ++it;
            int curr_deeg = 0;
            get_num(it, curr_deeg, str);
            deegs[curr_x] = curr_deeg;
        }
    }
    Monomial(const int& coef_tmp, const std::vector<int>& deegs_tmp) : coef(coef_tmp), deegs(deegs_tmp) {}
    int get_coef() const {
        return coef;
    }
    int operator[](size_t i) const {
        return deegs[i];
    }

    bool is_equal(const Monomial& other) const {
        for (size_t i = 0; i < 26; ++i) {
            if (deegs[i] != other[i]) {
                return 0;
            }
        }
        return 1;
    }
    bool is_div(const Monomial& other) const {
        size_t i = 0;
        for (size_t i = 0; i < 26; ++i) {
            if (deegs[i] < other[i]) {
                return false;
            }
        }
        return true;
    }

    auto begin() const {
        return deegs.begin();
    }
    auto end() const {
        return deegs.end();
    }
    auto rbegin() const {
        return deegs.rbegin();
    }
    auto rend() const {
        return deegs.rend();
    }
    Monomial& operator *=(int x) {
        coef *= x;
        return *this;
    }
    Monomial operator *(int x) const {
        Monomial tmp_m = *this;
        return tmp_m *= x;
    }
    Monomial& operator *=(const Monomial& other) {
        coef *= other.get_coef();
        for (size_t i = 0; i < 26; ++i) {
            deegs[i] += other[i];
        }
        return *this;
    }
    Monomial operator * (const Monomial& other) const {
        Monomial tmp_m = *this;
        return tmp_m *= *this;
    }
    Monomial& operator +=(const Monomial& other) {
        // check for equal
        coef += other.get_coef();
        return *this;
    }
    Monomial operator + (const Monomial& other) const {
        Monomial tmp_m;
        return tmp_m += other;
    }
    Monomial operator/(const Monomial& other) const {
        std::vector<int> tmp_deegs(26);
        for (size_t i = 0; i < 26; ++i) {
            tmp_deegs[i] = deegs[i] - other[i];
        }
        return { coef / other.get_coef(), tmp_deegs };
    }
    bool operator==(const Monomial& other) const {
        return is_equal(other);
    }
    bool operator!=(const Monomial& other) const {
        return !(is_equal(other));
    }
private:
    int coef;
    std::vector<int> deegs;
};
////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////
class MonomialOrder {
public:
    MonomialOrder() {}
    MonomialOrder(const std::vector<std::function<bool(const Monomial&, const Monomial&)>>& tmp_mon_ord) {
        orders = tmp_mon_ord;
    }
    void add_order(const std::function<bool(const Monomial&, const Monomial&)>& func) {
        orders.push_back(func);
    }
    std::function<bool(const Monomial&, const Monomial&)>& operator[](size_t i) {
        return orders[i];
    }
    bool compair_less(const Monomial& mon1, const Monomial& mon2) const {
        for (auto func : orders) {
            if (func(mon1, mon2) != func(mon2, mon1)) {
                return func(mon1, mon2);
            }
        }
        return 0;
    }
    // !(не правльно так должно быть без первого параметра в методе) [правильно так как это Order класс]
    bool compair_less_or_eq(const Monomial& mon1, const Monomial& mon2) const {
        for (auto func : orders) {
            if (func(mon1, mon2) != func(mon2, mon1)) {
                return func(mon1, mon2);
            }
        }
        return 1;
    }
private:
    std::vector<std::function<bool(const Monomial&, const Monomial&)>> orders;
};
bool lexicograph(const Monomial& mon1, const Monomial& mon2) {
    for (size_t i = 0; i < 26; ++i) {
        if (mon1[i] != mon2[i]) {
            return mon1[i] < mon2[i];
        }
    }
    return 0;
}
////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////
class Polynomial {
public:
    Polynomial() {}
    Polynomial(const Monomial& other_mon) {
        monoms.push_back(other_mon);
    }
    std::vector<Monomial>& get_monoms() {
        return monoms;
    }
    size_t size() const {
        return monoms.size();
    }
    void dell_same_monoms_or_add(const Monomial& other) {
        for (auto& monom : monoms) {
            if (monom.is_equal(other)) {
                monom += other;
                return;
            }
        }
        monoms.push_back(other);
    }
    // Same fragments
    Polynomial& operator +=(const Monomial& other) {
        bool diff = true;
        for (size_t i = 0; i < monoms.size(); ++i) {
            if (monoms[i].is_equal(other)) {
                monoms[i] += other.get_coef();
                diff = false;
            }
        }
        if (diff) {
            monoms.push_back(other);
        }
        return *this;
    }
    Polynomial operator + (const Monomial& other) const {
        Polynomial tmp_p = *this;
        return tmp_p += other;
    }
    Monomial operator[](const size_t& i) const {
        return monoms[i];
    }
    Polynomial& operator += (const Polynomial& other) {
        for (const auto& mon_from_other : other.monoms) {
            *this += mon_from_other;
        }
    }
    Polynomial operator + (const Polynomial& other) const {
        Polynomial tmp_pol = *this;
        return tmp_pol += other;
    }
    Polynomial& operator *= (const int x) {
        for (auto& mon : monoms) {
            mon *= x;
        }
        return *this;
    }
    Polynomial operator *(const int x) const {
        Polynomial tmp_p = *this;
        return tmp_p *= x;
    }
    Polynomial& operator *= (const Polynomial& other) {
        Polynomial tmp_p = *this;
        monoms.clear();
        for (const auto& mon_from_this : tmp_p.monoms) {
            for (const auto& mon_from_other : other.monoms) {
                *this += mon_from_this * mon_from_other;
            }
        }
        return *this;
    }
    Polynomial operator *(const Polynomial& other) const {
        Polynomial tmp_p = *this;
        return tmp_p *= other;
    }
    auto begin() const {
        return monoms.begin();
    }
    auto end() const {
        return monoms.end();
    }
    Polynomial& operator -= (const Polynomial& other) {
        for (size_t i = 0; i < size(); ++i) {
            monoms[i] *= -1;
        }
        *this += other;
        for (size_t i = 0; i < size(); ++i) {
            monoms[i] *= -1;
        }
        return *this;
    }
    Polynomial operator - (const Polynomial& other) const {
        Polynomial tmp_p = *this;
        return tmp_p -= other;
    }
    void sort_pol(const MonomialOrder& ord) {
        std::sort
        (
            monoms.begin()
            , monoms.end()
            , [&ord](Monomial const& mon1, Monomial const& mon2)
            {
                return ord.compair_less(mon1, mon2);
            }
        );
    }
    size_t size() {
        return monoms.size();
    }

    auto rbegin() const {
        return monoms.rbegin();
    }
    auto rend() const {
        return monoms.rend();
    }

private:
    std::vector<Monomial> monoms;
};

int main() {
    Monomial f1("+1a1b2"), f2("-1a1b1c2"), f3("+1b4c1");
    Monomial a1("+2a2b2"), a2("+2a1b4c1"), a3("+1b4c6");
    Monomial g1("+1a2b2"), g2("+2a1b4c1"), g3("+1a4c6");
    Polynomial f, g, ans, a;
    f += f1;
    f += f2;
    f += f3;
    g += g1;
    g += g2;
    g += g3;
    a += a1;
    a += a2;
    a += a3;
    a -= g;
    return 0;
}

Добавил в класс Monomial:

Monomial& operator *=(const int& x) {
    coef *= x;
    return *this;
}
Monomial operator *(const int& x) const{
    Monomial tmp_m = *this;
    return tmp_m *= x;
}

А в Polynomial изменил Polynomial& operator -= (const Polynomial& other) на:

   Polynomial& operator -= (const Polynomial& other) {
        for (size_t i = 0; i < size(); ++i) {
            monoms[i] *= -1;
        }
        *this += other; // Почему-то, если убрать эту строчку то ошибка исчезает
        for (size_t i = 0; i < size(); ++i) {
            monoms[i] *= -1;
        }
        return *this;
    }

Но все равно не работает :(

Answer 1

Забыл return в Polynomial& operator += (const Polynomial& other);

READ ALSO
Qt. Установка фокуса кнопке, при обработке keyPressEvent у Key_Down-Key_Up

Qt. Установка фокуса кнопке, при обработке keyPressEvent у Key_Down-Key_Up

Есть форма, на которой расположены несколько кнопок с именами "up", "down", и тд

72
Как получить список всех разделов в Windows?

Как получить список всех разделов в Windows?

Хочу получить информацию о дисках и разделах в windowsИспользовала функцию GetLogicalDrives(), но оттуда я могу взять только именованные разделы

126
Как разместить текст по центру на SVG Path

Как разместить текст по центру на SVG Path

Когда я использую SVG, чтобы нарисовать цикл и поместить текст, то как центрировать текст на SVG Path?

83