矢量迭代器不是dereferencable(矢量内矢量)

时间:2012-04-05 16:12:45

标签: c++ vector struct dereference ofstream

我正在尝试将这些向量中的一些数据写入文本文件。当我运行代码时,它返回运行时错误。 CategoryProductCartCustomerAddress都是struct,其成员每个get_member都会返回。

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();

返回运行时错误“Vector iterator not dereferencable”

以下是struct Address的样子:

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;


};

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;           
};

2 个答案:

答案 0 :(得分:3)

您忘了初始化vector<Address>::iterator n;

答案 1 :(得分:1)

您正在声明迭代器n,但不会将其初始化为可解除引用的值。从您的更新中,您似乎想要打印与客户关联的Address;所以你可以通过m引用的客户访问,而不是单独的迭代器:

write_custs << m -> get_cust_id() << '\t';
write_custs << m -> get_address().get_zip_code() << endl;

此外,将每个迭代器的范围放在其循环中可能是个好主意;这比每次在外部范围内声明一个新的更容易出错:

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.

还有其他几点:

  • 您应该使用!=而不是<来与end()迭代器进行比较; <对某些类型的迭代器不起作用;
  • 您应该将新行写为'\n'而不是endl; endl刷新文件缓冲区,强制该文件在那时写入磁盘,这可能非常慢。