Reputation: 5
I am trying to write some data from these vectors onto text files. When I run the code it returns a run time error. Category
, Product
, Cart
, Customer
and Address
are all struct
with members that each get_member
returns.
ofstream write_cats;
write_cats.open("catprd.dat", ios::out, ios::trunc);
vector<Category>::iterator i;
write_cats << cats.size() << endl;
for (i = cats.begin(); i < cats.end(); i++) {
write_cats << i -> get_catid() << '\t';
}
vector<Product>::iterator j;
write_cats << prods.size() << endl;
for (j = prods.begin(); j < prods.end(); j++) {
write_cats << j -> get_prodid() << '\t';
write_cats << j -> get_prodprice() << endl;
}
write_cats.close();
ofstream write_carts;
write_carts.open("carts.dat", ios::out, ios::trunc);
vector<Cart>::iterator k;
write_carts << carts.size() << endl;
for (k = carts.begin(); k < carts.end(); k++) {
write_carts << k -> get_cartid() << '\t';
write_carts << k -> get_day() << endl;
}
vector<Cart_item>::iterator l;
write_carts << cart_items.size() << endl;
for (l = cart_items.begin(); l < cart_items.end(); l++) {
write_carts << l -> get_cartitemid() << '\t';
write_carts << l -> get_qty() << endl;
}
write_carts.close();
ofstream write_custs;
write_custs.open("custs.dat", ios::out, ios::trunc);
vector<Customer>::iterator m;
vector<Address>::iterator n;
write_custs << custs.size() << endl;
for (m = custs.begin(); m < custs.end(); m++) {
write_custs << m -> get_cust_id() << '\t';
write_custs << n -> get_zip_code() << endl;
}
write_custs.close();
Returns run time error "Vector iterator not dereferencable"
Here is how struct Address
looks like:
using namespace std;
#pragma once
#include <string>
struct Address {
public:
int get_st_number() const{return st_number;}
int get_zip_code() const{return zip_code;}
string get_st_name() const{return st_name;}
Address(){}
Address (int num, string name, int zip)
: st_number(num), st_name(name), zip_code(zip) {}
private:
int st_number;
int zip_code;
string st_name;
};
and struct Customer
:
struct Customer {
public:
Address get_address() const{return addr;}
int get_cust_id() const{return cust_id;} customer id
string get_name() const{return cust_name;}
Customer (int id, string n, Address a)
: cust_id(id), cust_name(n), addr(a) {}
string display_addr() const {
std::cout<<setw(15)<<cust_name<<" ";
std::cout<<setw(15)<<cust_id<<" ";
return string();
}
private:
int cust_id;
string cust_name;
Address addr;
};
Upvotes: 0
Views: 491
Reputation: 254531
You are declaring an iterator n
, but not initialising it to a dereferencable value. From your update, it looks like you want to print the Address
associated with the customer; so you'd access via the customer referred to by m
, rather than a separate iterator:
write_custs << m -> get_cust_id() << '\t';
write_custs << m -> get_address().get_zip_code() << endl;
Also, it might be a good idea to scope each iterator inside its loop; that's less error-prone than declaring a new one in the outer scope each time:
for (vector<Whatever>::const_iterator i = stuff.begin(); i != stuff.end(); ++i) {
// do stuff with "i"
}
// "i" is no longer available - no danger of accidentally using it again.
And a couple of other points:
!=
rather than <
to compare with the end()
iterator; <
doesn't work for some types of iterator;'\n'
rather than endl
; endl
flushes the file buffer, forcing the file to be written to disk at that point, which can be extremely slow.Upvotes: 1