C++中重载运算符编译但崩溃



我制作了一个MegaInt类,它可以处理非常大的数字,并重载了一些运算符。不幸的是,我被卡住了,我的代码崩溃了。

每个MegaInt ojb都有一个向量值和一个布尔符号。数字的每个位置都放在向量中(即4325是向量值={5,2,3,4}),以及其符号(+或-)是1还是0。

这里有一些代码。。。

        #include <vector>
        #include <string>
        using namespace std;
        class MegaInt{
            friend class main;
            private:
                vector<int> value;
                bool sign; //true is pos, false is neg
            public:
                MegaInt(vector <int> x, bool y);
                MegaInt(string s);
                MegaInt(const MegaInt& m);
                MegaInt & operator*=(const MegaInt &rhs);
                #include <iostream>
                using namespace std;
                #include "MegaInt.h"
                #include <math.h>
                MegaInt::MegaInt(string s){
                    int pos = s.length()-1;
                    while (pos >= 0){
                        if(pos == 0 && s[pos] == '-'){
                            sign = false;
                            break;
                        }
                        else if (pos == 0 && s[pos] == '+'){
                            sign = true;
                            break;
                        }
                        else{
                            sign = true;
                        }
                        if(s[pos] >= 48 && s[pos] <= 57)
                            value.push_back(s[pos]-48);
                        else{
                            value.clear();
                            break;
                        }
                        pos --;
                    }
                    chopoffleadingOs();
                }
                MegaInt::MegaInt(const MegaInt& m){
                    value = m.value;
                    sign = m.sign;
                }
                MegaInt operator*(const MegaInt& x, const MegaInt& y){
                    int multi = 0;
                    int temp;
                    vector<int> total;
                    for(int i = x.value.size()-1; i>=0; --i){
                        for(int j = y.value.size()-1, k = 0; j>=0; --j, ++k){
                            temp = x.value[i] * y.value[j];
                            if (total.size() <= (i + multi + 1))
                                                     total.resize(i + multi + 1 + 1);
                                                total[i + multi] += (temp % 10);
                                                temp = (temp - total[i]) / 10; 
                                                total[i + multi + 1] += temp;
                                                     }
                                                     multi++;
                                                    }
                                        reverse(total.begin(), total.end());
                return newTotal;
                }

我似乎主要被困在了乘法这一部分。剩下的我想我得到了。

谢谢,Noah

问题可能就在这里:

total[i+multi]+=(8%10);

您使用total矢量,但尚未为其分配内存。这可以通过resize函数完成:

total.resize(MAX_SIZE);

发现意外崩溃的一个好方法是使用调试器。如果你在调试器中运行程序,它会在出现问题时停止,这样你就可以检查变量,看看崩溃的原因和原因

编辑:

如果您事先不知道total矢量的大小,则必须动态调整其大小:

if (total.size() <= (i + multi + 1))
    total.resize(i + multi + 1 + 1);  // An extra +1 because vectors are zero-indexed
total[i+multi]+=(8%10);
// ...

如果可能的话,最好提供一个完全可编译的样本;让人们在有时间的情况下更容易尝试样本。不要把所有的东西都贴出来——只是展示问题所必需的最低限度。

使用调试器-通过嵌套的for循环跟踪代码,并检查向量中的索引值。

(在使用Eclipse时,双击左边距在一行上设置断点。用于运行到断点。高亮显示要监视的变量或表达式并右键单击所选内容,然后从菜单中选择"监视"将其添加到监视窗口。

在源代码方面,Eclipse可能有点于事无补。如果你正在学习C++,我会推荐一些更友好的东西——我个人会推荐Visual C++Express(它是免费的)。)

最新更新