使用结构向量解决边界错误
Address Boundary Error With Vector of Structs
我正在尝试设置结构向量,并通过读取数据文件向结构添加数据。代码正确解析数据文件,因为我能够使用 songList[y]
而不是 songData[x].song
打印出值。当我运行它时,我总是收到错误SIGSEGV (Address boundary error)
.我做错了什么?
#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;
}
这是数据文件(歌曲.txt):
Song 1;Band 1;Min 1;Sec 1;Album 1
Song 2;Band 2;Min 2;Sec 2;Album 2
Song 3;Band 3;Min 3;Sec 3;Album 3
Song 4;Band 4;Min 4;Sec 4;Album 4
Song 5;Band 5;Min 5;Sec 5;Album 5
分段错误背后的原因是您使用变量x
的方式。
考虑一下:
while(getline(infile, tempLine)) {
songData.push_back(Song()); // songData.size() = 1 after reading first line
istringstream ss(tempLine);
string temp;
现在考虑以下循环的第二次迭代(即读取Band 1
): 此时,x = 1
和 songList.size() = 1
。
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;
}
}
}
以下是更正后的代码。我已经删除了矢量songList
因为我认为这不是必需的。另外,我还更改了变量x
和y
的用法。
#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;
// Open file
ifstream infile;
infile.open("songs.txt");
// Add data to vector
string tempLine;
while (getline(cin, tempLine)) {
songData.push_back(Song());
istringstream ss(tempLine);
string temp;
while(getline(ss, temp, ';')){
if(y == 0) {
songData[x].song = temp;
cout << "Song:t" << songData[x].song << endl;
} else if (y == 1) {
songData[x].band = temp;
cout << "Band:t" << songData[x].band << endl;
} else if (y == 2) {
songData[x].minutes = temp;
cout << "Min:t" << songData[x].minutes << endl;
} else if (y == 3) {
songData[x].seconds = temp;
cout << "Sec:t" << songData[x].seconds << endl;
} else if(y == 4) {
songData[x].album = temp;
cout << "Album:t" << songData[x].album << endl;
} else {
cout << "Error!" << endl; // Will never run
}
y++;
if(y == 5) {
y = 0;
}
}
x++;
}
return 0;
}
相关文章:
- 如何在 macOS 卡塔琳娜上解决此错误?
- 我在解决此错误时遇到问题.我正在努力在主函数中传递数组
- 如何解决分段错误错误C++
- 如何在构建对象堆栈时解决转换错误?
- 如何使用QOpenGLWidget解决GL_INVALID_OPERATION错误
- 如何解决 comiler 错误:'v_bias'不是命名空间名称
- 任何人都可以在带有JNI的Android Studio上帮助我解决此错误,是否缺少一些内容?
- 如何解决此错误(将WCHAR_T转换为BSTR)
- 如何从McIsendstring()中解决此错误并使其播放声音
- 您好,有人可以帮助我解决此错误吗?
- 您能帮我在Android Studio中解决这个错误吗?
- 如何解决声明错误
- 由于不变成员而具有可能已删除的默认构造函数,因此解决编译器错误
- Strcasecmp.无法解决的错误消息
- 枚举作为基类的模板参数总是会因未解决的错误而失败
- C++:如何在 do while 循环中解决字符错误
- 有关如何解决此错误的建议
- 循环向量引起的地址边界错误
- 如何解决Linux错误 - "cannot find -lGL"?
- 使用结构向量解决边界错误