nested class

can anyone tell me whats wrong with my code, the compiler says:
line 25: expected identifier before numeric constant
line 25: expected "," or "..." before numeric constant
the same for line 26

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
34
  #include <iostream>
#include <iomanip>
using namespace std;

class Time{
private:
    unsigned short hours, minutes;
public:
    Time (unsigned short h, unsigned short m) {hours = h; minutes  = m;};
    void printTime () {
    cout << "\n\t" << setw(2) << setfill('0') << hours << ":" << setw(2) << minutes << ":" << endl;};
};
class Date{
private:
    unsigned short day, month, year;
public:
    Date (unsigned short d, unsigned short m, unsigned short y) {day = d; month = m; year = y;};
    void printDate () {
    cout << "\n\t" << setw(2) << setfill('0') << day << "/" << setw(2) << month << "/" << year << endl;};
};

class Flights{
private:
    string departureAirport, arrivalAirport;
    Time departureTime(1,1), arrivalTime(1,1), journeyLength(0,0);
    Date departureDate(1,1,1), arrivalDate(1,1,1);
    float cost;
public:
    Flights(string deptAir, string arrivAir, float costt){departureAirport = deptAir; arrivalAirport = arrivAir; cost = costt;};
    };

int main(){
    return 0;
}
1
2
3
4
5
6
7
8
9
class Flights {
private:
    string departureAirport, arrivalAirport;
    Time departureTime = Time(1, 1), arrivalTime = Time(1, 1), journeyLength = Time(0, 0);
    Date departureDate = Date(1, 1, 1), arrivalDate= Date(1, 1, 1);
    float cost;
public:
    Flights(string deptAir, string arrivAir, float costt) { departureAirport = deptAir; arrivalAirport = arrivAir; cost = costt; };
};


Time departureTime(1,1) is interpreted as a function declaration.
Last edited on
Are you using a C++14 compiler?

Older compilers don't allow initializing members as you are doing in lines 25-26.
i am using codeblox
i was told that because i have constructor for class Time that taking 2 values
i have to give it values when i am using it in Flights
if i comment out the constructors for Time and Date and take out values in line 25 and 26 it compiles.
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
34
#include <iostream>
#include <iomanip>
using namespace std;

class Time{
private:
    unsigned short hours, minutes;
public:
   // Time (unsigned short h, unsigned short m) {hours = h; minutes  = m;};
    void printTime () {
    cout << "\n\t" << setw(2) << setfill('0') << hours << ":" << setw(2) << minutes << ":" << endl;};
};
class Date{
private:
    unsigned short day, month, year;
public:
    //Date (unsigned short d, unsigned short m, unsigned short y) {day = d; month = m; year = y;};
    void printDate () {
    cout << "\n\t" << setw(2) << setfill('0') << day << "/" << setw(2) << month << "/" << year << endl;};
};

class Flights{
private:
    string departureAirport, arrivalAirport;
    Time departureTime, arrivalTime, journeyLength;
    Date departureDate, arrivalDate;
    float cost;
public:
    Flights(string deptAir, string arrivAir, float costt){departureAirport = deptAir; arrivalAirport = arrivAir; cost = costt;};
    };

int main(){
    return 0;
}


but i need the constructors, so what do i do now?
i am using codeblox


Code::Blocks is not a compiler. It's an IDE. You can find it packaged with g++, so perhaps that's the compiler you're using. If so, you should specify that you want the compiler to use C++11. If you do so, the code I posted above should compile.

Otherwise, you can just explicitly initialize them with the Flights constructor:

1
2
3
4
Flights (string deptAir, string arrivAir, float costt) : departureTime(1,1), arrivalTime(1,1), // etc
{
    // ...
}
The problem was not your explicit constructors (line 9,17), but that you did not have a default constructor.

Since you appeared to be trying to instantiate those classes with default values, you have a couple of choices:
1) Provide a default constructor that initializes the object.
9
10
11
    //  Initialize members with default values
    Time ()  : hours(1), minutes(1)
    {}


2) Provide default values on your existing constructor.
9
10
11
    //  Constructor with default values
     Time (unsigned short h = 1, unsigned short m = 1)  : hours(h), minute (m)
    {}

Don't use both methods. You will get an error because the compiler will consider the constructors ambiguous.



Last edited on
I see how I didn't think it through. unfortunately the time is not default, there will be 8 flight objects and they all will have different time and date
Then pass the Date and Time through Flight's constructor.
Last edited on
would you mind showing me an example please
1
2
3
4
5
6
7
8
9
10
Flights (Date depdate, Time deptime, Date arrdate, Time arrtime, string deptAir, string arrivAir, float costt)
{ departureDate = depdate;
  departureTime = deptime;
  arrivalDate = arrdate;
  arrivalTime = arrtime;
  //  journeyLength should be calculated
  departureAirport = deptAir; 
  arrivalAirport = arrivAir; 
  cost = costt;
}
it works if i take out the constructors from Time and Date. but i need to be able to input 2 values for time and 3 for date.

or if i leave it the way you did this this, how will i declare objects for class flight.
I need to input all the values in when declaring the objects (except journey length that has to be calculate as you stated)

this obviously doesn't work
flight1((1,1,1), (1,1), (1,1,1), (1,1), "ORK", "LTH", 480);



Try this:
 
flight1(Date(1,1,1), Time(1,1), Date(1,1,1), Time(1,1), "ORK", "LTH", 480);


I ended up doing this:

1
2
3
4
5
6
7
8
9
10
class Flights{
private:
    string departureAirport, arrivalAirport;
public:
    Time departureTime = Time(1,1), arrivalTime = Time (1,1), journeyLength = Time(0,0);
    Date departureDate = Date (1,1,1), arrivalDate = Date(1,1,1);
    float cost;
public:
    Flights(string deptAir, string arrivAir, float costt){departureAirport = deptAir; arrivalAirport = arrivAir; cost = costt;};
    };
Topic archived. No new replies allowed.