cin 上的段错误。尝试了最后的事情。糟糕的 gdb
Seg Fault on cin. Tried the endl thing. Lousy at gdb
我遇到了一个我不明白的奇怪段错误。
will@will-mint ~/code/byun-sp15 $ g++ -g all_pairs.cpp
will@will-mint ~/code/byun-sp15 $ ./a.out
Please enter filename:
table.txt
Segmentation fault
正如您从 cout staements 中看到的那样,它在 cin>>filename;
中出现段错误这是主要函数:
#include <iostream>
#include <fstream>
#include <vector>
#include <sstream>
using std::cin;
using std::cout;
using std::endl;
using std::string;
using std::ifstream;
using std::vector;
int main()
{
int n;
string filename;
cout << "Please enter filename: " << endl;
cin >> filename;
cout << "FLAG FLAG FLAG";
ifstream fin(filename.c_str());
if (!fin.is_open()) {
cout << "ERROR: file not found. Exiting..." << endl;
return -1;
}
fin >> n;
vector<Table> tables;
int temp;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
fin >> temp;
tables[0].data[i][j] = temp;
}
}
for (int i = 1; i < n-1; i++) {
tables.push_back(calc_next_table(tables, i, n));
}
return 0;
}
我真的觉得自己像个白痴,我以前从来没有遇到过这种情况。我找到了this,但是没有解决。我什至尝试了 gdb(我真的不知道如何使用),但我得到的只是:
Program received signal SIGSEGV, Segmentation fault.
0x0000000000401ae8 in std::vector<std::vector<int, std::allocator<int> >, std::allocator<std::vector<int, std::allocator<int> > > >::operator[] (this=0x0, __n=0) at /usr/include/c++/4.8/bits/stl_vector.h:771
771 { return *(this->_M_impl._M_start + __n); }
(gdb) list
766 * out_of_range lookups are not defined. (For checked lookups
767 * see at().)
768 */
769 reference
770 operator[](size_type __n)
771 { return *(this->_M_impl._M_start + __n); }
772
773 /**
774 * @brief Subscript access to the data contained in the %vector.
775 * @param __n The index of the element for which data should be
这看起来像一个向量在做一些时髦的事情,但我什至还没有声明我的向量!有什么想法吗?
编辑:我喜欢@neilson 的回答,但以下内容仍然会引发段错误:
fin >> n;
vector<Table> tables;
Table t;
tables.push_back(t);
int temp;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
fin >> temp;
tables[0].data[i][j] = temp;
}
}
cout << "FLAG FLAG FLAG" << endl;
Table 有一个元素,称为 data
的 vector<vector<int>>
。我也应该 post 吗?
我认为问题出在这里:
fin >> n;
vector<Table> tables;
int temp;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
fin >> temp;
tables[0].data[i][j] = temp; // HERE
}
}
当您访问它时,向量是空的。
做
tables.push_back(..something..)
首先。
像这样:
fin >> n;
vector<Table> tables;
Table someTable; // Create a table
tables.push_back(someTable); // Put it in the vector
int temp;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
fin >> temp;
tables[0].data[i][j] = temp;
}
}
编辑:
既然你告诉Table也有一个vector,那么你需要改成:
tables[0].data[i][j] = temp;
也可以使用 push_back。
除非先添加元素,否则无法访问向量中的元素。
如果Table有成员:
vector<vector<int>> data;
你也许可以这样做:
fin >> n;
vector<Table> tables;
Table someTable; // Create a table
tables.push_back(someTable); // Put it in the vector
int temp;
for (int i = 0; i < n; i++) {
tables[0].data.push_back(vector<int>()); // create the i'th vector and insert it
for (int j = 0; j < n; j++) {
fin >> temp;
tables[0].data[i].push_back(temp); // Use push_back
}
}
注意:我没有测试过这个,因为我不在编译器附近......但我认为这样就可以了。
我遇到了一个我不明白的奇怪段错误。
will@will-mint ~/code/byun-sp15 $ g++ -g all_pairs.cpp
will@will-mint ~/code/byun-sp15 $ ./a.out
Please enter filename:
table.txt
Segmentation fault
正如您从 cout staements 中看到的那样,它在 cin>>filename;
中出现段错误这是主要函数:
#include <iostream>
#include <fstream>
#include <vector>
#include <sstream>
using std::cin;
using std::cout;
using std::endl;
using std::string;
using std::ifstream;
using std::vector;
int main()
{
int n;
string filename;
cout << "Please enter filename: " << endl;
cin >> filename;
cout << "FLAG FLAG FLAG";
ifstream fin(filename.c_str());
if (!fin.is_open()) {
cout << "ERROR: file not found. Exiting..." << endl;
return -1;
}
fin >> n;
vector<Table> tables;
int temp;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
fin >> temp;
tables[0].data[i][j] = temp;
}
}
for (int i = 1; i < n-1; i++) {
tables.push_back(calc_next_table(tables, i, n));
}
return 0;
}
我真的觉得自己像个白痴,我以前从来没有遇到过这种情况。我找到了this,但是没有解决。我什至尝试了 gdb(我真的不知道如何使用),但我得到的只是:
Program received signal SIGSEGV, Segmentation fault.
0x0000000000401ae8 in std::vector<std::vector<int, std::allocator<int> >, std::allocator<std::vector<int, std::allocator<int> > > >::operator[] (this=0x0, __n=0) at /usr/include/c++/4.8/bits/stl_vector.h:771
771 { return *(this->_M_impl._M_start + __n); }
(gdb) list
766 * out_of_range lookups are not defined. (For checked lookups
767 * see at().)
768 */
769 reference
770 operator[](size_type __n)
771 { return *(this->_M_impl._M_start + __n); }
772
773 /**
774 * @brief Subscript access to the data contained in the %vector.
775 * @param __n The index of the element for which data should be
这看起来像一个向量在做一些时髦的事情,但我什至还没有声明我的向量!有什么想法吗?
编辑:我喜欢@neilson 的回答,但以下内容仍然会引发段错误:
fin >> n;
vector<Table> tables;
Table t;
tables.push_back(t);
int temp;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
fin >> temp;
tables[0].data[i][j] = temp;
}
}
cout << "FLAG FLAG FLAG" << endl;
Table 有一个元素,称为 data
的 vector<vector<int>>
。我也应该 post 吗?
我认为问题出在这里:
fin >> n;
vector<Table> tables;
int temp;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
fin >> temp;
tables[0].data[i][j] = temp; // HERE
}
}
当您访问它时,向量是空的。
做
tables.push_back(..something..)
首先。 像这样:
fin >> n;
vector<Table> tables;
Table someTable; // Create a table
tables.push_back(someTable); // Put it in the vector
int temp;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
fin >> temp;
tables[0].data[i][j] = temp;
}
}
编辑:
既然你告诉Table也有一个vector,那么你需要改成:
tables[0].data[i][j] = temp;
也可以使用 push_back。
除非先添加元素,否则无法访问向量中的元素。
如果Table有成员:
vector<vector<int>> data;
你也许可以这样做:
fin >> n;
vector<Table> tables;
Table someTable; // Create a table
tables.push_back(someTable); // Put it in the vector
int temp;
for (int i = 0; i < n; i++) {
tables[0].data.push_back(vector<int>()); // create the i'th vector and insert it
for (int j = 0; j < n; j++) {
fin >> temp;
tables[0].data[i].push_back(temp); // Use push_back
}
}
注意:我没有测试过这个,因为我不在编译器附近......但我认为这样就可以了。