繁体   English   中英

C ++错误-预期在'。'之前的主表达式 令牌| 在结构上

[英]C++ Error - expected primary-expression before '.' token| IN STRUCTURE

我只是想说我仍在学习C ++,所以我从关于结构的模块开始,虽然我不了解所有内容,但我认为我的理解是正确的。 编译器不断给我的错误是:

错误:“。”之前的预期主表达式 令牌|

#include <iostream>
#include <fstream>
using namespace std;

struct catalog
{
    char title[50];
    char author[50];
    char publisher[30];
    int yearpublish;
    double price;
};

int main()
{
    catalog book;
    char again;

    fstream fbook;
    fbook.open("book.dat", ios::out | ios::binary);

    do
    {
        //Read data about the book
        cout << "Enter the following data about a book:\n";
        cout << "Title:";
        cin.getline (book.title,50);
        cout << "Author:";
        cin.getline (book.author,50);
        cout << "Publisher name:";
        cin.getline (book.publisher,30);
        cout << "Year publish:";
        cin >> book.yearpublish;
        cin.ignore();
        cout << "Price:";
        cin >> book.price;
        cin.ignore();

        //write the contents to the binary file
        fbook.write((char*)&catalog , sizeof(book));

        cout << "Do you want to enter another record?(Y/N)";
        cin >> again;
        cin.ignore();
    } while (again == 'Y' || again == 'y');

    fbook.close();
    return 0;
}

请帮忙。

您在这行在这里做什么

fbook.write((char*)&catalog , sizeof(book));

我认为应该

 fbook.write((char*)&book , sizeof(book));

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM