由于 if 语句,C++ 程序崩溃

c++ program crashing because of if statment

本文关键字:程序 崩溃 C++ if 语句 由于      更新时间:2023-10-16

我认为我的c ++程序崩溃是因为if语句。我正在使用 MinGW 编译器,没有给出任何错误。我不知道为什么会出现错误。我的生成函数中的 if 语句对我来说看起来不错。我将字符串与向量字符串的实例进行比较。

这是CPP代码:

#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <cstdlib>
#include <ctime>
#include "Insultgenerator_0hl14.h"
using namespace std;

FileException::FileException(const string& m) : message(m){}
string& FileException::what(){ return message;}
NumInsultsOutOfBounds::NumInsultsOutOfBounds(const string& m) : message(m){}
string& NumInsultsOutOfBounds::what(){ return message;}
InsultGenerator::InsultGenerator(const InsultGenerator& ) {};
InsultGenerator::InsultGenerator(){};
void InsultGenerator::initialize() {
    int cols(0);
    srand ( time(NULL));
    string words ;
    string filename("InsultsSource.txt");
    ifstream filetoread(filename.c_str());
    if(filetoread.fail()){
        throw FileException("File not read.");
    }
     while(filetoread >> words){
         if(cols==0){
            colA.push_back(words);
            cols++;
         } else if(cols==1){
            colB.push_back(words);
             cols++;
         }else{
             colC.push_back(words);
             cols= cols -2;
         }

     }

    //for (int i=0;i<50;i++){
    //  cout << "  "<< colA[i];
    //}
}



string InsultGenerator::talkToMe() const{
    string Thou = "Thou";
    string a= Thou + " " + colA[(rand()%50)] + " "  + colB[rand()%50] + " " + colC[rand()%50] +"!" ;
    //cout << a << endl;
    return a;
};//end talkToMe
vector<string> InsultGenerator::generate(const int num){
    if (num<0){
    throw NumInsultsOutOfBounds("You must be insulted at least once");
} else if (num >10000 ){
    throw NumInsultsOutOfBounds("You are being insulted too many times!");
}
    vector<string> insultList;
    string list;
     for(int i=0; i<num;i++ ){
         list = talkToMe();
        if(list != insultList[i]){
             //insultList.push_back(list);
             //cout << insultList[i]<< endl;
        }
     }

return insultList;
    };//end generate
    //int InsultGenerator::generateAndSave(const string filename, const int n) const{
    //};//end generateAndSave
int main(){
    InsultGenerator ig;
ig.initialize();
ig.talkToMe();
ig.generate(10);

    }

这是头文件:

#ifndef INSULTGENERATOR_0HL14_H_
#define INSULTGENERATOR_0HL14_H_
#include <string>
#include <vector>
using namespace std;
class InsultGenerator{
public:
    InsultGenerator();
    InsultGenerator(const InsultGenerator&);
    void initialize() ;
    string talkToMe() const;
    vector<string> generate(const int) ;
    int generateAndSave (const string, const int) const;
private:
    vector<string> colA;
    vector<string> colB;
    vector<string> colC;
};
class FileException{
public:
    FileException(const string&);
    string& what();
private:
    string message;
};
class NumInsultsOutOfBounds{
public:
    NumInsultsOutOfBounds(const string &);
    string& what();
private:
    string message;
};
#endif

一旦你在向量上调用operator[],你就在尝试访问向量的元素。在这种情况下,向量为空,这会导致未定义的行为。

if(list != insultList[i]){

在尝试访问向量之前,请确保使用某些值初始化此向量。

您要查找的索引 ( i ) 必须小于向量insultList.size()的大小(因为索引从 0 开始)

您声明insultList属于vector<string>类型:

vector<string> insultList;

然后,您正在尝试访问insultList元素:

     if(list == insultList[i]){
         //insultList.push_back(list);
         //cout << insultList[i]<< endl;
    }

但是在这两者之间,你实际上没有为insultList添加任何东西 - 它有多少元素? 尝试访问insultList[i]是指导致程序崩溃的未分配内存。