由于 if 语句,C++ 程序崩溃
c++ program crashing because of if statment
我认为我的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]
是指导致程序崩溃的未分配内存。
相关文章:
- 程序崩溃并显示"std::out_of_range"错误
- 试图创建流或fopen时程序崩溃
- 应用程序崩溃并显示"symbol _ZdlPvm, version Qt_5 not defined in file libQt5Core.so.5 with link time reference"
- 如何找出应用程序崩溃的原因 - Win 10 LTSB
- 操纵安卓相机的深度图导致应用程序崩溃
- 为什么当我尝试搜索双链表中第一个数据条目之外的数据时,程序崩溃了?
- DLL Made with CMake 使程序崩溃
- 程序崩溃使用boost::asio
- 调用 free() 有时会导致程序崩溃
- 邮件加密程序崩溃
- 调用 java 的回调() 时应用程序崩溃.由于 detatchThread 而获得运行时错误
- 比较迭代器会使程序崩溃,而不会在自定义气泡排序实现中出现错误
- For 循环在尝试读取数组 c++ 时程序崩溃
- 即使有 0 个错误,Getter 似乎也会使程序崩溃
- Windows桌面程序保存您的计算机会话 - 基于程序崩溃时的恢复会话
- 无法访问的代码如何导致我的程序崩溃?
- 矢量迭代器在尝试调用函数时使我的程序崩溃
- QCompleter set模型使应用程序崩溃
- 使用唯一指针调用函数会使我的程序崩溃
- 删除SDL_PollEvent时程序崩溃