有人能帮助我吗?
我无法解决的两个错误:
这是一个标题文件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,我是编码的新手。)
答案 0 :(得分:2)
第二个错误是第一个错误的结果。给出的第一个错误是:
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
数组。它使很多代码很多更简单。
答案 1 :(得分:0)
问题是你的结构名为“date”,它有一个名为“date”的成员变量。编译器抛出错误,因为它们的名称相同。
考虑到这一点的一个简单方法是,如果你做了一种
int int;
由于int是一个类型,因此不能将变量命名为与类型相同的名称。