C++ 结构向量的地址边界错误

C++ 结构向量的地址边界错误,c++,C++,我试图建立一个结构向量,并通过读取数据文件向结构添加数据。代码正确地解析了数据文件,因为我能够使用songList[y]而不是songData[x].song打印出值。当我运行它时,我总是得到错误SIGSEGV(地址边界错误)。我做错了什么 #include <fstream> #include <iostream> #include <sstream> #include <vector> using namespace std; struct

我试图建立一个结构向量,并通过读取数据文件向结构添加数据。代码正确地解析了数据文件,因为我能够使用
songList[y]
而不是
songData[x].song打印出值。当我运行它时,我总是得到错误
SIGSEGV(地址边界错误)
。我做错了什么

#include <fstream>
#include <iostream>
#include <sstream>
#include <vector>

using namespace std;

struct Song{
    string song;
    string band;
    string minutes;
    string seconds;
    string album;
};

vector <Song> songData; // Vector the data will eventually end up

// MAIN FUNCTION

int main(){
    int x = 0, y = 0;
    vector <string> songList;
    // Open file
    ifstream infile;
    infile.open("songs.txt");
    // Add data to vector
    string tempLine;
    while(getline(infile, tempLine)){
        songData.push_back(Song());
        istringstream ss(tempLine);
        string temp;
    while(getline(ss, temp, ';')){
        songList.push_back(temp);
        if(x == 0){
                songData[x].song = songList[y];
                cout << "Song:\t" << songData[x].song << endl;
            }else if(x == 1){
                songData[x].band = songList[y];
                cout << "Band:\t" << songData[x].band << endl;
            }else if(x == 2){
                songData[x].minutes = songList[y];
                cout << "Min:\t" << songData[x].minutes << endl;
            }else if(x == 3){
                songData[x].seconds = songList[y];
                cout << "Sec:\t" << songData[x].seconds << endl;
            }else if(x == 4){
                songData[x].album = songList[y];
                cout << "Album:\t" << songData[x].album << endl;
            }else{
                cout << "Error!" << endl; // Will never run
        }
        x++;
        y++;
        if(x == 5){
            x = 0;
        }
    }
    }
    return 0;
}

分段错误背后的原因是您使用变量
x
的方式

考虑这一点:

while(getline(infile, tempLine)) {
    songData.push_back(Song()); // songData.size() = 1 after reading first line
    istringstream ss(tempLine);
    string temp;

现在考虑下面循环的第二次迭代(即读<代码>带1 ):在这一点上,<代码> x=1 < /代码>和<代码>歌曲列表。siz()= 1 < /代码> ./p>

    while(getline(ss, temp, ';')) {
        songList.push_back(temp); // songList.size() = 2, after reading "Band 1"
        if(x == 0) {
            songData[x].song = songList[y];
            cout << "Song:\t" << songData[x].song << endl; 
        } else if(x == 1) {
            songData[x].band = songList[y]; // Undefined behaviour which may lead to Seg. Fault because songData.size() = 1 and you are trying to access 2nd element of songData.
            cout << "Band:\t" << songData[x].band << endl;
        }
        ...
        x++;
        y++;
        if(x == 5) {
            x = 0;
        }
    }
}

分段错误背后的原因是您使用变量
x
的方式

考虑这一点:

while(getline(infile, tempLine)) {
    songData.push_back(Song()); // songData.size() = 1 after reading first line
    istringstream ss(tempLine);
    string temp;

现在考虑下面循环的第二次迭代(即读<代码>带1 ):在这一点上,<代码> x=1 < /代码>和<代码>歌曲列表。siz()= 1 < /代码> ./p>

    while(getline(ss, temp, ';')) {
        songList.push_back(temp); // songList.size() = 2, after reading "Band 1"
        if(x == 0) {
            songData[x].song = songList[y];
            cout << "Song:\t" << songData[x].song << endl; 
        } else if(x == 1) {
            songData[x].band = songList[y]; // Undefined behaviour which may lead to Seg. Fault because songData.size() = 1 and you are trying to access 2nd element of songData.
            cout << "Band:\t" << songData[x].band << endl;
        }
        ...
        x++;
        y++;
        if(x == 5) {
            x = 0;
        }
    }
}

您似乎使用了相同的变量(
x
)作为
songData
的索引,以及作为要填充的songData字段的鉴别器。其中一个应该是
y
,我想?可能也不应该同时增加这两个值。您可能希望使用调试器或它单步执行代码。欢迎使用StackOverflow。作为一个需要考虑的问题:你能用更少的代码来演示你的问题吗?e、 如果只是
struct-Song{string-Song;},你还会崩溃吗没有剩下的?如果是这样,您应该发布较短的代码,并保持其重点!读一读什么是a。您也可以使用该按钮编辑您的问题。您似乎使用了相同的变量(
x
)作为
songData
的索引,以及作为要填充的songData字段的鉴别器。其中一个应该是
y
,我想?可能也不应该同时增加这两个值。您可能希望使用调试器或它单步执行代码。欢迎使用StackOverflow。作为一个需要考虑的问题:你能用更少的代码来演示你的问题吗?e、 如果只是
struct-Song{string-Song;},你还会崩溃吗没有剩下的?如果是这样,您应该发布较短的代码,并保持其重点!读一读什么是a。您也可以使用该按钮编辑您的问题。@DylanConklin回答了您的问题吗?@DylanConklin回答了您的问题吗?