C++分段 - 故障问题

0 c++ segmentation-fault

我的程序崩溃了,这对我来说很好,当然我的程序说不然,这让我很困惑.

我正在处理的这个功能片段:

        for(int k = 0; k < dictionary[k].size(); k++)
        {
            //"i" represents the fragment's first character
            //while "k" represents the dictionary first character
            if(fragments[i][j] == dictionary[k][j]) {
                token++;
                cout << token << endl;
            }
        }
Run Code Online (Sandbox Code Playgroud)

可能是问题所在.当我调试问题时,调试器转到代码段的第一行:

    for(int k = 0; k < dictionary[k].size(); k++)
Run Code Online (Sandbox Code Playgroud)

然后在我尝试下一次时崩溃.在调试器中,此窗口在我的代码块中打开:

Signal Received

Program Received Singal SIGEGV, segmentation fault. Do you want to view backtrace?
Run Code Online (Sandbox Code Playgroud)

我点击了,这对我来说似乎是武断的.

有谁知道我做错了什么?

如果需要回溯(窗口显示Call Stack),我会在以后根据需要进行编辑

编辑:这是整个功能,我认为没有必要

void Language::compare()
{
    int para = getParameters(0); //eg. 3

    int valid = para;
    int token = 0;

    for(int i = 0; i < para; i++)
    {
        //If the string is creater than 2 characters
        if(fragments[i].length() > 1) {
            for(int j = 0; j < fragments[i].length(); j++)
            {
                //Checking if that character match in dictionary
                for(int k = 0; k < para; k++) //Changed and now works,
                {
                    //"i" represents the fragment's first character
                    //while "k" represents the dictionary first character
                    if(fragments[i][j] == dictionary[k][j]) { //But now this line crashes
                        token++;
                        cout << token << endl;
                    }
                }
                if(token == 0) {
                    break;
                }
            }
        }
        else {
        //...
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

字典和片段在类"语言"中声明,它们都是向量.

Vot*_*ple 10

我怀疑你打算在那里使用dictionary[k].size()循环控制的一部分,因为循环是迭代的k.你是说dictionary.size()或者dictionary[i].size()也许?


Sim*_*son 7

这一行:

for(int k = 0; k < dictionary[k].size(); k++)
Run Code Online (Sandbox Code Playgroud)

看起来确实令人怀疑.你确定你不想循环到字典数组/集合的大小?

如果你发布dictionary它的定义可能会帮助我们提出一些具体的建议.