gpt4 book ai didi

c++ - 结构和数组的初始值设定项太多

转载 作者:行者123 更新时间:2023-11-28 01:57:46 25 4
gpt4 key购买 nike

有人可以帮助我吗?

我无法解决的两个错误:

  • 太多的初始化器和
  • 在“日期”之前输入

这是一个头文件book.h

struct date
{

int date; >>>>the type(s preceding'date'),don't know what is this??

int month;

int year;
};

typedef struct {

char book_name[51];

char book_ID[7];

char author[31];

char publisher[31];

struct date book_date;

double price;


}BOOK;

这是一个.cpp文件

void main()

{

int i;

BOOK bk_detail[10] ={

{"Harry Potter","B1001","JK ROWLING","Little Brown",{30,6,1997},56.00},

{"Me Before You","B1002","Jojo Moyer","Penguin Group",{3,6,2004},58.00},

{"Game Of Throne","B1003","George RR Martin","Snow Dany",{4,4,1990},69.00},

{"Maze Runner","B1004","James Dashner","Chicken Mouse",{3,3,2003},55.00},

{"Papertown","B1005","John Green","Dutton Penguin",{16,8,2008},47.00},

{"Twilight","B1006","Stephenie Meyer","Little Brown",{12,7,2005},56.00},

{"The Hunger Game","B1007","Suzanne Collins","Scholastic Corporation",{3,9,2006},69.00},

{"No Man Sky","B1008","Nathan Stuart","Littlefinger Group",{4,8,2014},55.00},

{"Resist the Temptation","B1009","Jon Snow","The Night Watch",{8,9,1998},69.00},

{"To Code","B1010","Daenerys Targaryen","Dragons BB",{5,3,1997},56.00}

};
FILE*books_detail=fopen("book_detail.bin","wb");
if (books_detail == NULL)
{
printf("ERROR...\n");
exit(-1);
}

fwrite(&bk_detail,sizeof(bk_detail[0]),10,books_detail);

fclose(books_detail);

}

我已经按照教程步骤操作了,但它似乎仍然有错误,为什么仍然有这两个错误。(PS,我是编码新手。)

最佳答案

第二个错误是第一个错误的结果。第一个错误,是给定的:

 struct date {

名为 date 的成员是构造函数 - 构造函数前面不能有类型。换句话说,您不能拥有与结构或类同名的成员变量。

解决方法是将其更改为:

      int day_of_month;

编辑:除了cpp.sh完全满意:

// Example program

struct Foo
{
int Foo;
};

int main()
{
Foo foo;
foo.Foo = 1;

return foo.Foo;
}

(具有相同的“问题”)。

强烈建议您不要使用char 数组,而是始终使用std::string。它使很多代码变得更简单。

关于c++ - 结构和数组的初始值设定项太多,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40590485/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com