C++11 当回路不断给出分段故障时

C++11 当回路不断给出分段故障时,c++11,segmentation-fault,C++11,Segmentation Fault,我在这个while循环中得到了一个分段错误,我不知道为什么。该程序是一个行代码计数器。while循环用于计算类中的行数,因此它只是将“{”添加到堆栈中,并在循环中出现“}”时将其弹出。当堆栈为空时,它应该退出while循环。下面是while循环,后面是完整的代码 int countObjectLines(int index){ stack<char> symbols; int i = index+1; int numberLines = 0;

我在这个while循环中得到了一个分段错误,我不知道为什么。该程序是一个行代码计数器。while循环用于计算类中的行数,因此它只是将“{”添加到堆栈中,并在循环中出现“}”时将其弹出。当堆栈为空时,它应该退出while循环。下面是while循环,后面是完整的代码

int countObjectLines(int index){
    stack<char> symbols;
    int i = index+1;
    int numberLines = 0;        
    symbols.push('{');
    while (!symbols.empty())
    {       
        string test(code[i]);
        for(int j = 0; j < test.size(); j++){               
            if(test[j] == '{'){
                symbols.push(test[j]);
            }

            else if(test[j] == '}' && symbols.top() == '{'){
                symbols.pop();                  
            }

            else{}
            numberLines++;
        }
        i++;
    }

    return numberLines;
}
这是完整的代码

#include <iostream>
#include <fstream>
#include <stack> 
#include <string>
#include <vector>
#include <sstream>
using namespace std;

//Lines of Code Iterator Class
class LOCitr{
public: 
    int count;
    vector<string> code;

    LOCitr()
    {
        count = 0;
    }

    ~LOCitr(){}

    //Trim White Space so line does not get counted
    string trimWhiteSpace(string const& str)
    {
        size_t first = str.find_first_not_of(' '); //find first character not a space
        if(first == string::npos) 
            return "";
        size_t last  = str.find_last_not_of(' '); //find the end of line before more whitespaces
        return str.substr(first, last-first+1); //create the string again with no white spaces
    }

    //Trim tab space from string
    string trimTab(string const& str)
    {
        size_t first = str.find_first_not_of('\t'); 
        if(first == string::npos) 
            return "";
        size_t last  = str.find_last_not_of('\t'); 
        return str.substr(first, last-first+1); //create the string again with no tab
    }

    //counter of lines
    int counter(string fileName)
    {
        string line;    
        string first_word = "";
        ifstream myfile (fileName.c_str());
        if (myfile.is_open())
        {
            while ( getline (myfile,line) )
            { 
                line = trimWhiteSpace(line);
                line = trimTab(line);
                if(line.empty()) //check for empty line
                    continue;     
                if(line[0] == '/' && line[1] == '/') //check for comment
                    continue;
                count++;
                code.push_back(line);
            }

            for (int i = 0; i < code.size(); i++){
                cout<< endl << "code["<<i<<"] "<<code[i];
                stringstream stream(code[i]);       
                stream >> first_word;
                if (first_word == "class" || first_word == "struct")
                    {
                        cout << endl << first_word << i;
                        cout << "objectLines: " << countObjectLines(i);
                    }
            }

            myfile.close();
        }
        else return 0; 



        return count;
    }

    int countObjectLines(int index){
        stack<char> symbols;
        int i = index+1;
        int numberLines = 0;        
        symbols.push('{');
        while (!symbols.empty())
        {       
            string test(code[i]);
            for(int j = 0; j < test.size(); j++){               
                if(test[j] == '{'){
                    symbols.push(test[j]);
                }

                else if(test[j] == '}' && symbols.top() == '{'){
                    symbols.pop();                  
                }

                else{}
                numberLines++;
            }
            i++;
        }

        return numberLines;
    }


};


int main () {

    int count = 0;
    string file = "";
    LOCitr countMe;
    char input = 'y';

    //while loop to keep entering files to check
    while (input == 'y'){
        cout << "Enter file name:" << "\t";
        cin >> file;

        count = countMe.counter(file);    

        cout << endl << count <<endl;

        cout << "Enter another file? Enter Y/N :" << "\t";

        cin >> input;
    }



    return 0;
}

您的代码似乎有一些问题:

你的循环条件是while!symbols.empty,但假设代码中的大括号已正确平衡,则符号堆栈不会变为空。那会发生什么?您将访问代码[i],其中i超出了代码的有效范围

你的计算数线=j-i+1;似乎是错误的,因为i用于索引行,j用于索引列。不知道如何减去这些,得到有意义的东西


我猜你在空集合上调用symbols.pop。对不起,else{symbols.pop;}实际上只是else{},我已将其删除,但不小心在太远的地方单击了cntrl+z。我在上面修改了它,我看到了numberLines的问题,我把它改成了numberLines++并在if-else语句之外进行了修改。但是关于牙套,你这是什么意思?我将左括号推进去开始,这样堆栈就有1个项目。之后,如果另一个左括号进来,它将推动它,如果一个右括号进来,它将弹出堆栈。因此,如果出现正确数量的右大括号,堆栈将清空,并且应该始终有一个相等的数字,因为它只是在一个对象代码块中进行迭代。首先,将某个对象推到堆栈上,使其不为空。如果你的输入文本有平衡的大括号,堆栈怎么会变成空的?再次感谢你的帮助,如果你查看代码,我实际上从左括号后面的那一行开始。我实际上刚刚更改了它,实际上只是将它改成了其他的东西,现在它可以工作了。再次感谢