C++ 方法给出由于c+中的分段错误导致的应用程序崩溃+;

C++ 方法给出由于c+中的分段错误导致的应用程序崩溃+;,c++,vector,while-loop,crash,segmentation-fault,C++,Vector,While Loop,Crash,Segmentation Fault,此简单循环方法无法运行,出现错误“应用程序崩溃”。当我在在线编译器中检查时,它给出了“分段错误(内核转储)”- string studies(setMatter ch) { string a = "LISTEN CAREFULLY!\n"; int i = 0; while (i <= ch.a.quantity()) { a += ch.a.getAns(i); i++; } return a; } 字符串研究(SETCH){ s

此简单循环方法无法运行,出现错误“应用程序崩溃”。当我在在线编译器中检查时,它给出了“分段错误(内核转储)”-

string studies(setMatter ch) {
  string a = "LISTEN CAREFULLY!\n";
  int i = 0;
  while (i <= ch.a.quantity()) {
    a += ch.a.getAns(i);
    i++;
  }
  return a;
}
字符串研究(SETCH){
string a=“仔细听!\n”;
int i=0;
而(我是的,你是

while(i<=ch.a.quantity()){

while(我感谢你,但是调用此方法时应用程序崩溃仍然存在。在打印a的值时,“仔细听”已打印,但之后应用程序崩溃。请帮助我。提前感谢!!@hemantkumar上面的函数没有打印任何内容。因此,您的崩溃可能在另一个函数中。是什么让您认为它在这个函数中?但是上面的函数返回a的值,并且起始行是“仔细听”.我已经用简单的方法打印了一张cout@hemantkumar很抱歉,但是我应该如何知道您之前没有提到的代码。无论如何,崩溃可能仍然是在该函数返回后发生的。如果我理解正确,那么该函数不会导致崩溃,因为您已经修复了

while(i<=ch.a.quantity()){
while(i<ch.a.quantity()){
for (int i = 0; i < ch.a.quantity(); i++) {
    a += ch.a.getAns(i);
}