我无法显示全部我已经读取二进制文件内容的矢量的内容。我可以访问此矢量的各个元素,但不能打印所有这些元素。我的代码去如下:将二进制文件的内容读入对象矢量
BankSystem::BankSystem(string file_name) {
vec_acc.reserve(10);
fstream acc_file(file_name, ios::in | ios::out | ios::binary);
for(int i=0; i<10; i++) {
acc_file.seekg(sizeof(Account)*i);
acc_file.read((char*)&vec_acc[i], sizeof(Account));
}
acc_file.close();
}
所以该构造是假设读取二进制文件的内容为Account
载体。
我知道数据已被正确读入Account
载体中,因为我可以在其上执行方法功能,如getName
或getBalance
。然而,当我尝试显示向量的内容:
void BankSystem::showAccounts() {
std::copy(vec_acc.begin(), vec_acc.end(), std::ostream_iterator<Account>
(cout, " "));
}
这以前工作时,我推了Account
对象到载体。但是,由于我已经添加了从二进制文件中将内容读入矢量的功能,所以我无法使其工作。
“Account”包含什么? – Galik
是否是POD?在你的代码中进行下面的编译:'static_assert(std :: is_pod :: value,“Account must be POD。”);' –
marcinj
@Galik我很困惑你正在问什么。它包含7个方法函数,6个数据成员和一个重载的'operator'。 'ostream&operator <<(ostream&os,const Account&acc){os <<“Account(Name:”<< acc.firstName +''+ acc.lastName <<“,Account Number:”<< acc.acc_num << “)”;返回操作系统} – quantik