Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/130.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 访问冲突读取位置0x012D363C_C++ - Fatal编程技术网

C++ 访问冲突读取位置0x012D363C

C++ 访问冲突读取位置0x012D363C,c++,C++,我正在使用visual studio 2013。我的项目是图书馆管理系统。在阅读我的文件后,它是访问的例外 class Book { string edition; string serialno; string shelfno; int date, month, year; public: Book(); Book(char name, char aname, string edit, int srno, int shfno); vo

我正在使用visual studio 2013。我的项目是图书馆管理系统。在阅读我的文件后,它是访问的例外

class Book {

    string edition;
    string serialno;
    string shelfno;
    int date, month, year;

public:
    Book();
    Book(char name, char aname, string edit, int srno, int shfno);
    void getbook();
    void showbook();
    void getdate();
    string bookname;
    string authorname;
};

Book::Book()
{
    bookname = "BOOKNAME";
    authorname = "AUTHORNAME";
    edition = "EDITION";
    serialno = "SERIALNO.";
    shelfno = "SHELFNO.";
}

void Book::showbook()
{
    cout << bookname << " ---- " << authorname << " ---- " << edition << "----  " << serialno << "----" << shelfno << endl;
}

void Librarysystem::showrecord()
{
    ifstream file;

    Book b;
    file.open("bookrecord.txt", ios::in);
    if (!file)
        cerr << "\n could not open file:";
    cout << "\t\t BOOK RECORD\n\n" << endl;
    while (!file.eof()) {
        b.showbook();
        file.read(reinterpret_cast<char*>(&b), sizeof(b));

        if (file.eof())
            file.close();
        //cerr << "\n could not read from file:";
    }
}
教材{
弦乐版;
字符串序列号;
弦谢尔夫诺;
int日期、月份、年份;
公众:
书();
图书(字符名、字符名、字符串编辑、int-srno、int-shfno);
void getbook();
void showbook();
void getdate();
字符串书名;
字符串authorname;
};
书
{
bookname=“bookname”;
authorname=“authorname”;
edition=“edition”;
serialno=“serialno。”;
shelfno=“shelfno。”;
}
void Book::showbook()
{

cout我们没有看到
Book
类的内容,但我强烈怀疑它使用了非POD(纯对象数据)成员,如
std::string

在这种情况下,您无法使用与纯C类型相同的技术对对象进行序列化,最重要的是反序列化

while (!file.eof())
{
    b.showbook();
    file.read(reinterpret_cast<char*>(&b), sizeof(b));
while(!file.eof())
{
b、 showbook();
read(reinterpret_cast(&b),sizeof(b));
因此,基本上,第一次,
b
没有用空值初始化/初始化,应该可以,但是在
file.read
调用之后,
b
保存以前的
b
状态,如果存在
std::string
对象,它会保存无效内存区域上的指针(你会尝试序列化指针吗?没有意义:这里也一样)


最简单的方法是在
Book
中编写适当且特定的序列化/反序列化方法(例如,通过重新定义
operator

如果对象不包含POD纯成员,则无法反序列化此类对象。能否向我们展示
Book
的定义?Book肯定不是POD类型。永远不要保存对象的状态,保存其字段值。您需要类内的序列化方法请学习缩进代码;它有助于我们调试,也有助于您。ev最好是定义运算符