可执行文件在运行时有错误

时间:2017-04-12 20:07:08

标签: c++

我有以下代码正确编译:

#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#include "INVESTMENT.h"
#include <vector>
using namespace std;


int main()
{


    ifstream inputFile("jroth.csv");
    string sHolder; //used as placeholder for string
    float fHolder; //used as placeholder for float
    double dHolder; ///used as placeholder for double

//  while (inputFile.good())
    vector<int> InvestVector;  //will hold class (Investment) which contains string, double, and float
    for (int i=0; i <= 7; i++)
    {
        Investment InvestVector[i]; //create new class for each line being pulled from .csv file
        getline(inputFile, sHolder, ',');  //pull in investment symbol as string
        InvestVector[i].setSymbol(sHolder); //store string in the class
        cout << InvestVector[i].getSymbol(); //verify string store properly
    }

    return 0;
}

程序运行后,可执行文件崩溃。有什么想法吗?

1 个答案:

答案 0 :(得分:0)

假设您使用的是带有可变长度数组的C ++编译器,这不是标准的C ++,那么这一行:

Investment InvestVector[i];

使用&#34; i&#34;创建一个数组。地方,编号从0到i-1。然后这两行:

InvestVector[i].setSymbol(sHolder);
cout << InvestVector[i].getSymbol();

将尝试使用位置编号&#34;我&#34;数组的数组,即一个超过数组末尾的数据。