Structure Initialization failing

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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
struct time
{
       int hour;   // Hour (24-hour clock) 0-23
       int minute; // 0-59
};

/************************************
*** AIRLINE RESERVATION STRUCTURE ***
************************************/
struct airline_reservation
{
       int flight_number;     // 5-digit #
       char orig_air_code[3]; // 3-character
       char dest_air_code[3]; // 3-character
       struct time depart_time;      // Departure Time
       struct time arrive_time;      // Arrival Time
};


int main()
{
    //Create data for 10 flights
    
    struct airline_reservation detroit_metro = {   
           { 12345, "ABC", "ZYX", {10, 0}, {15, 0} }
    };//*****PROBLEM**** 

Where the code is marked PROBLEM, the compiler generates this error:
brace-enclosed initializer used to initialize `int'

I'm guessing I used an invalid format for the structure initialization. Is this the correct way to initialize the structure?
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.
'struct' is optional and I prefer to use it.

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.
Last edited on
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 :
1
2
3
struct airline_reservation detroit_metro = {
    12345, "AB", "ZY", {10, 0}, {15, 0} 
    };



**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. ***
Last edited on
Yep I just noticed that along with the character error and fixed it a second before you posted. Thank you ;)
Last edited on
Why don't you create constructors?
eg:
 
time::time(int h, int m):hour(h),minute(m) {}
Last edited on
I don't even know what the above code means... could you explain it. I know about class constructors but not structure constructors.
Last edited on
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 
Last edited on
Okay,

Is this the expanded form of the constructor or are they slightly different from a class constructor?
1
2
3
4
5
time(int h, int m)
{
    hour = h;
    minute = m;
}
Last edited on
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.
Last edited on
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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <iostream>

using namspace std;

struct Foo {
   Foo() { cout << "Foo default constructed" << endl; }
   Foo( int ) { cout << "Foo( int ) called" << endl; }
   Foo& operator=( int ) { cout << "Foo operator= called" << endl; return *this; }
};

struct A {
   A( const Foo& f ) {
      foo = f;
   }

  Foo foo;
};

struct B {
   B( const Foo& f ) : foo( f ) {}
   Foo foo;
};

int main() {
    cout << "Making foo..." << endl;
    Foo f( 4 );

    cout << "Performing A test:" << endl;
    A a( f );

    cout << "Performing B test:" << endl;
    B b( f );
}


And note the difference in output.
Topic archived. No new replies allowed.