Can someone help me?
Two errors that I couldn't solve:
This is a header file 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;
This is a .cpp
file
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);
}
I've followed the tutorial steps but it appears that it still have errors,why that's still have these two error.(PS, I'm new to coding.)
The second error is a consequence of the first. The first error, is that given:
struct date {
a member called date
is a constructor - and constructors can't be preceeded by a type. To put it another way, you can't have a member variable with the same name as the struct or class.
The fix is to change it to:
int day_of_month;
Edit : Except that cpp.sh is entirely happy with:
// Example program
struct Foo
{
int Foo;
};
int main()
{
Foo foo;
foo.Foo = 1;
return foo.Foo;
}
(which has the same "problem").
I would strongly recommend that rather than using arrays of char
, you use std::string
throughout. It makes a lot of code much simpler.
The issue is that your struct is named "date" and it has a member variable named "date". The compiler throws an error because they are named the same.
An easy way to think about this is if you made a type of
int int;
Since int is a type, you cannot name variable the same name as a type.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.