递归期间堆栈溢出错误

时间:2014-12-10 08:32:01

标签: c++ loops recursion structure stack-overflow

我正准备开始研究这个项目。快到了!我想....我只是有一个堆栈溢出错误来解决。这是代码:

using namespace std;

struct runner{
public:
    int position;
    string time;
    int age;
    string sex;
    string gender;
    string firstName;
    string lastName;
    string city;
    string state; 


runner(){
    runner r1;
    string dataChunk;
    int ageTotal = 0;
    double ageAverage = 0.0;
    int ageCount = 0;
    int femaleAlabama = 0;
    int femaleOverForty = 0;
    int femaleHuntsville = 0;
    int femaleCount = 0;
    double femaleAgeAverage = 0.0;

    ifstream inFile("C:\\Users\\Anthony\\Desktop\\cmarathon.csv");


        getline(inFile, dataChunk, ',');
        r1.position = atoi(dataChunk.c_str());

        getline(inFile, dataChunk, ',');
        r1.time = dataChunk;

        getline(inFile, dataChunk, ',');
        r1.age = atoi(dataChunk.c_str());
        ageTotal = +age;
        ageCount = +1;

        getline(inFile, dataChunk, ',');
        r1.sex = dataChunk;
        if(sex == "f" || "F")
            femaleCount++;
            femaleAgeAverage++;

        getline(inFile, dataChunk, ',');
        r1.gender = dataChunk;

        getline(inFile, dataChunk, ',');
        r1.firstName = dataChunk;

        getline(inFile, dataChunk, ',');
        r1.lastName = dataChunk;

        getline(inFile, dataChunk, ',');
        r1.city = dataChunk;

        getline(inFile, dataChunk, ',');
        r1.state = dataChunk;

        if(sex == "f" || "F" && age > 40)
            femaleOverForty++;

        if(sex == "f" || "F" && city == "Huntsville")
            femaleHuntsville++;

        if(sex == "f" || "F" && state == "Al" || "AL")
            femaleAlabama++;
        cout<<r1.position<<" "<<r1.time<<" "<<r1.age<<" "<<r1.sex<<" "
            <<r1.gender<<" "<<r1.firstName<<" "<<r1.lastName<<" "<<r1.city
            <<" "<<r1.state<<endl;}

};




int main(){

int i;
    for(i = 1; i <1343; i++){
        runner();
    }







    system("PAUSE");
    return 0;

}

这里的目标是遍历.csv表并将数据拉入结构中。然后我可以用这些数据来计算各种事情,比如女性的平均年龄等等。有什么建议吗?

编辑:

以下是我尝试运行程序时收到的错误代码片段 My file name got cut off, but that's it for the dialogue

1 个答案:

答案 0 :(得分:1)

问题
不要在构造函数中创建runner r1;,这会导致无限递归。

解决方案
您可以将r1设置为静态或对现有对象的引用。例如runner &r1 = *this;