我有下面的类,它有一个vector of objects
。当我使用类对象读取向量的值时,程序崩溃了。我什至试图放入一个try block
,但控件没有进入catch block
。
这本书类:
class Book
{
public:
char isbn[11];
char name[50];
char author_name[50];
char pub_name[50];
char edition[10];
float cost;
char dept[10];
int count;
char id[8];
};
这是 Library 类,其中包含一个vector of class Book
class Library
{
public:
char l_name[50];
char admin_name[50];
int phone_no;
char location[50];
vector<Book> v;
};
这是主要功能
int main()
{
Library lib; //LIBRARY OBJECT
Book b; //BOOK OBJECT
fstream fp("library1.bin",ios::in | ios::binary); //OPEN FILE IN READ MODE
if(!fp) cout<<"reading failed...";
try
{
fp.read(reinterpret_cast<char *>(&lib),sizeof(lib)); //READ LIBRARY DETAILS
if(fp.bad() == true) cout<<"Read failure..."<<endl;
fp.close();
cout<<"Enter ISBN number:"; cin>>b.isbn; //READ ISDN NUMBER FROM USER
for(int i=0;i<lib.v.size();i++) //CHECK WHETHER BOOK ALREADY EXISTS
{
Book b1 = lib.v[i];
if(strcmp(b1.isbn,b.isbn) == 0)
{
lib.v[i].count++;
}
}
}
catch(exception& e) //CATCH ANY EXCEPTIONS
{
cout<<"Error:"<<e.what()<<endl;
}
catch(...) //CATCH ANY UNCAUGHT EXCEPTIONS
{
cout<<"Unknown error occured:";
}
}
当我试图检查 ISDN 号码时,程序崩溃了。