I have written a program for an exercise in a book I have. The program uses structures to store airline reservation data.
The problem occurs when trying to compile the source file.
This {} means a block of code only.
Don't use 'struct' when declaring an object.
You gotta either init one variable after another or create a constructor function, the concept and theory of which is too big to tell you.
Never mind.. I figured out the problem. I was using too many curly braces. This is the prototype for an array and I forgot to take the array braces out.
You have too many braces. The way it is written, everthing in this set
of braces {12345, "ABC", "ZYX", {10, 0}, {15, 0} } belongs to flight_number;. Hence you are getting an error.
It should be :
**NOTICE also that I have shortened your strings to AB and ZY. You have declared the arrays only to be 3 characters. This 3 characters includes the end of string terminating zero.
You either have to shorten the string like I have done OR increase the array size to prevent buffer overflow. ***
That is a class constructor with initializer list
( a struct is a class with public as default )
1 2 3 4 5 6 7 8
struct time
{
int hour;
int minute;
time(int h, int m):hour(h),minute(m) {}//initializes hour with the value of h and minute with minute the value of m
};
time mytime(12,34);//creates a time with hour=12 and minute=34
Using an inizializer list ( :hour(m),... ) should be better than assigning values on the initializer body as if you use the initializer list the members constructors are called on the same time as the class constructor is called but the result should be the same for built in types
They are roughly the same in concept but the expanded form is slower. Initializer lists are always preferred.
Although "struct" is optional (and for that matter "class" too) in your above case, you should avoid writing it, the reasons of which have to do with generic programming.
@jsmith: Okay, I didn't know the expanded forms were slower
How come I have to include 'struct' in my airline reservation structure? If I omit 'struct' before the 'time' variable the program doesn't recognize the structure as a datatype.
You need "struct" if you are compiling the code with a C compiler. You don't need it if you are compiling with C++. Note that some compilers use 'C' mode for files named *.c and C++ for .C or .cpp.
The reason I said what I said is this. Try this example: