执行错误。当我从data.dat读取数据到user :: user时,我收到内存错误。我应该尝试解决它 [英] Execute error.when I read data from data.dat to user::user I received a memory error.which way should I try to solve it
本文介绍了执行错误。当我从data.dat读取数据到user :: user时,我收到内存错误。我应该尝试解决它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
#include < iostream >
#include < fstream < span class =code-keyword>>
#include < span class =code-keyword>< vector >
class 预订
{
public :
Book();
Book(std :: string set_book_name,std :: string set_ISBN, bool set_check);
Book( const char * set_book_name, const char * set_ISBN, bool set_check);
Book( const Book& one);
private :
std :: string book_name;
std :: string ISBN;
bool check;
};
class 用户
{
public :
User();
private :
void read_in_data(空隙跨度>);
std :: vector< Book>列表;
};
Book :: Book():book_name( ),ISBN ( ),检查( true )
{
}
Book :: Book( const char * set_book_name, const char * set_ISBN, bool set_check):book_name(set_book_name),ISBN(set_ISBN),check(set_check)
{
}
预订: :Book( const Book& one)
{
book_name = one.book_name;
ISBN = one.ISBN;
check = one.check;
}
User :: User()
{
read_in_data();
}
void User :: read_in_data( void )
{
std :: ifstream ins;
ins.open( data.dat);
if (ins.fail())
{
std :: cout<< 无法读取文件data.dat。\ n;
退出( 1 );
}
预订临时;
/ * while(!ins.eof())
{
ins。 read(reinterpret_cast< char *>(& temp),sizeof(Book));
list.push_back(temp);
} * /
while (ins.read(reinterpret_cast< char *>(& temp), sizeof (书)))
{
list.push_back(temp);
}
ins.close();
return ; // ********此处有问题。
}
int main()
{
/ * 标准::矢量<书>书; // LINE:75
std :: ofstream outs;
outs.open(data.dat);
if(outs.fail())
{
std :: cout<< 文件无法写入。\ n;
退出(1);
}
book.push_back(Book(asd,123,false));
book.push_back(Book(asd,1234,false));
book.push_back(Book(asd,finally,false));
std :: vector< Book> :: iterator p = book.begin();
while(p!= book.end())
outs.write(reinterpret_cast< char *>(&(* p ++)),sizeof(Book));
outs.close(); * / // LINE:89
用户用户; // LINE :: 90
return 0 ;
}
我的尝试:
我的编程步骤是:
首先:我注意行:: 90,然后执行该程序将数据写入文件data.dat。
第二:我注意行:: 75到行:: 89,然后执行程序从data.dat读取数据到对象User :: user,此时,给出了内存错误。
那么,我的英语语法很糟糕,我希望你能尝试理解我的意图,我真的需要你的帮助。
解决方案
您不能以这种方式序列化/反序列化包含< code>
std :: string的类。例如,请参阅 c ++ - 序列化包含std :: string的类 - 堆栈溢出 [ ^ ]。
#include<iostream>
#include<fstream>
#include<vector>
class Book
{
public:
Book();
Book(std::string set_book_name, std::string set_ISBN, bool set_check);
Book(const char* set_book_name, const char* set_ISBN, bool set_check);
Book(const Book& one);
private:
std::string book_name;
std::string ISBN;
bool check;
};
class User
{
public:
User();
private:
void read_in_data(void);
std::vector<Book> list;
};
Book::Book() :book_name(""), ISBN(""), check(true)
{
}
Book::Book(const char* set_book_name, const char* set_ISBN, bool set_check) : book_name(set_book_name), ISBN(set_ISBN), check(set_check)
{
}
Book::Book(const Book& one)
{
book_name = one.book_name;
ISBN = one.ISBN;
check = one.check;
}
User::User()
{
read_in_data();
}
void User::read_in_data(void)
{
std::ifstream ins;
ins.open("data.dat");
if (ins.fail())
{
std::cout << "File data.dat can't be read.\n";
exit(1);
}
Book temp;
/* while (!ins.eof())
{
ins.read(reinterpret_cast<char *>(&temp), sizeof(Book));
list.push_back(temp);
}*/
while (ins.read(reinterpret_cast<char *>(&temp), sizeof(Book)))
{
list.push_back(temp);
}
ins.close();
return; //********PROBLEM HERE.
}
int main()
{
/* std::vector<Book> book; //LINE:75
std::ofstream outs;
outs.open("data.dat");
if (outs.fail())
{
std::cout << "File can't be written.\n";
exit(1);
}
book.push_back(Book("asd", "123", false));
book.push_back(Book("asd", "1234", false));
book.push_back(Book("asd", "finally", false));
std::vector<Book>::iterator p = book.begin();
while (p!=book.end())
outs.write(reinterpret_cast<char*>(&(*p++)), sizeof(Book));
outs.close();*/ //LINE:89
User user; //LINE::90
return 0;
}
What I have tried:
My programming steps is:
First:I note line::90,then execute this program to write data to file data.dat.
Second:I note line::75 to line::89,then execute program to read data from data.dat to object User::user,at this time,memory error was given.
By the way,My English grammer is bad,I hope you can try to understand my intention,I really need your help.
解决方案
You cannot serialize/deserialize a class containing <code>
std::string that way. See, for instance c++ - Serializing a class which contains a std::string - Stack Overflow[^].
这篇关于执行错误。当我从data.dat读取数据到user :: user时,我收到内存错误。我应该尝试解决它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文