[英]Segmentation fault using vectors in C++?
我正在尋找已編入代碼的分段錯誤。 它基本上出現,當我的函數parseRecord(string)
生成一個向量並且我想打印出其中一個元素時。 我已經縮小到那個點,但找不到錯誤。 向量是 Record 對象,它們是通過讀取文件創建的,每行包含一行數據。
每當我創建類的對象(不創建向量)時,我都可以很好地打印元素。 它僅在我使用創建向量的 parseRecord(string) 函數時出現。
我還嘗試在函數本身中打印它,結果相同。 所以我認為這不是范圍問題。
#include <iostream>
#include <fstream>
#include <iomanip>
#include <vector>
#include "record.hpp"
ifstream parsefile(string filename);
std::vector<Record> parseRecord(string line);
int main(int argc, char const *argv[])
{
std::vector<Record> dataSet = parseRecord("data.dat");
Record record("D11101001", "Max", "Muestermann",
10239, "fictionalmarkt.com", "23.12.19", "11:11:00");
cout << record;
for(std::vector<Record>::const_iterator i = dataSet.begin(); i != dataSet.end(); ++i){
cout << *i << endl;
}
return 0;
}
//METHOD-IMPLEMENTATION
//parse-file definition - Test if file is accessible
ifstream parsefile(string filename)
{
ifstream inFile(filename);
if (!inFile)
{
cerr << "File could not be opened" << endl;
exit(-1);
}
cout << "Input-file is readable!" << endl;
return inFile;
}
//parse-record definition - read file, add to vector<Record> and return vector
std::vector<Record> parseRecord(string filename)
{
vector<Record> dataSet;
ifstream inFile = parsefile(filename);
string line;
while (getline(inFile, line))
{
stringstream linestream(line);
string accountNb;
string firstName;
string lastName;
string amountStr; //format 100 = 1.00€
string merchant;
string date;
string time;
long double amount;
getline(linestream, accountNb, '|');
getline(linestream, firstName, '|');
getline(linestream, lastName, '|');
getline(linestream, amountStr, '|');
getline(linestream, merchant, '|');
getline(linestream, date, '|');
getline(linestream, time, '\n');
try
{
amount = stold(amountStr);
}
catch (const std::exception &e)
{
std::cerr << e.what() << '\n'
<< "Conversion error";
}
amount *= 100.0; //to correct the decimal format
Record recordEntry(accountNb, firstName, lastName, amount, merchant, date, time);
//cout << recordEntry << endl;
dataSet.push_back(recordEntry);
}
return dataSet;
}
記錄.hpp
class Record
{
private:
string accountNb;
string firstName;
string lastName;
long double amount; //format 100 = 1.00€
string merchant;
string date;
string time;
public:
Record(string, string, string, long double, string, string, string);
~Record();
friend ostream& operator<<(ostream&, const Record&);
};
記錄.cpp
#include "record.hpp"
#include <ostream>
Record::Record(string accountNb, string firstName, string lastName,
long double amount, string merchant, string date, string time)
{
this->accountNb = accountNb;
this->firstName = firstName;
this->lastName = lastName;
this->amount = amount;
this->merchant = merchant;
this->date = date;
this->time = time;
}
Record::~Record()
{
}
ostream &operator<<(ostream &os, const Record &rec)
{
os << right;
os << setw(15) << "Account Nb:" << setw(50) << rec.getAccountNb() << endl;
os << setw(15) << "First Name:" << setw(50) << rec.getFirstName() << endl;
os << setw(15) << "Last Name:" << setw(50) << rec.getLastName() << endl;
try
{
os << showbase;
os << setw(15) << "Amount:" << setw(50);
os.imbue(std::locale("de_DE.UTF-8"));
os << put_money(rec.getAmount(), true) << endl;
}
catch (const std::exception &e)
{
std::cerr << e.what() << "locale not supported system";
}
os << setw(15) << "Merchant:" << setw(50) << rec.getMerchant() << endl;
os << setw(15) << "Date:" << setw(50) << rec.getDate() << endl;
os << setw(15) << "Time:" << setw(50) << rec.getTime() << endl;
}
結果控制台打印:
Account Nb: D11101001
First Name: Max
Last Name: Muestermann
Amount: 102,39 EUR
First Name: Jonny
Last Name: Doe
Amount: 80,38 EUR
Merchant: markt.de
Date: 25.12.19
Time: 11:11:19
Segmentation fault (core dumped)
所以它在第一行之后退出。
一個錯誤是您沒有從此函數返回值:
ostream &operator<<(ostream &os, const Record &rec)
不從聲明為返回值的函數返回值是未定義的行為。
ostream &operator<<(ostream &os, const Record &rec)
{
//...
return os; // <-- you are missing this line
}
以上是明顯的錯誤,但另一個潛在的錯誤是在您對有效double
測試中。 您捕獲了一個異常,但隨后您的代碼繼續運行,就好像double
是有效的,而實際上它是未初始化的。
double amount; // <--uninitialized.
//..
try
{
amount = stold(amountStr);
}
catch (const std::exception &e)
{
std::cerr << e.what() << '\n' << "Conversion error";
}
amount *= 100.0; // <-- behavior undefined if exception above this line was thrown.
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.