array object

Hi all,

I have a class Driver and a constructor. Originally I had this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class Driver: virtual public Person{
protected:
    string idNum, address, licenceDate,licenceType;
    float kilomiters;
    bool booked;
public:
    void changeStatus(){booked = true;};
    Driver() {};
    Driver (string nam, string ph, string id, string add, string licenceD, string licenceT, float kil, bool b){name = nam; phoneNo = ph; idNum = id;
    address = add; licenceDate = licenceD; licenceType = licenceT; kilomiters = kil; booked = b;};
};

int main(){
    Driver driver1("Tom Daly", "087-6543210", "1234567A", "14 Green St., Cork", "12/08/2008", "B", 23231, false);
    Driver driver2("Anne O'Brien", "086-5432109", "2345678B", "\"Beach View\", Kinsale", "09/12/2011", "D", 11980, false);
    Driver driver3("James Twomey", "085-4321098", "3456789B", "14, French St., Cork", "14/11/2010", "D1", 18414, false);
return 0;
}

etc.. there is 6 of them.
But then I decided to change it an array object and so I did this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class Driver: virtual public Person{
protected:
    string idNum, address, licenceDate,licenceType;
    float kilomiters;
    bool booked;
public:
    void changeStatus(){booked = true;};
    Driver() {};
    Driver (string nam, string ph, string id, string add, string licenceD, string licenceT, float kil, bool b){name = nam; phoneNo = ph; idNum = id;
    address = add; licenceDate = licenceD; licenceType = licenceT; kilomiters = kil; booked = b;};
}driver[6];

int main(){
    Driver driver[0]("Tom Daly", "087-6543210", "1234567A", "14 Green St., Cork", "12/08/2008", "B", 23231, false);
    Driver driver[1]("Anne O'Brien", "086-5432109", "2345678B", "\"Beach View\", Kinsale", "09/12/2011", "D", 11980, false);
    Driver driver[2]("James Twomey", "085-4321098", "3456789B", "14, French St., Cork", "14/11/2010", "D1", 18414, false);
    Driver driver[3]("Mary O'Neill", "089-8765432", "4567890C", "23 Castle Road, Youghal", "11/02/2014", "B", 12669, false);
    Driver driver[4]("Brendan Brown", "083-2109876", "5678901D", "98 Nuns Walk, Cork", "01/04/2007", "D", 23864, false);
    Driver driver[5]("Vincent Coy", "087-8901234", "6789012E", "\"Green Valley\", Cobh", "03/04/1998", "D1", 3416, false);
return 0;
}


and now i get an error:

bad array initializer
conflict declaration 'Driver driver[1]'
'driver' has previous declaration as 'Driver driver[0]'
conflict declaration 'Driver driver[2]'
'driver' has previous declaration as 'Driver driver[0]' etc..

I understand that I am declaring it wrong, possibly because I am declaring it twice. But I don't know how to fix it. Can someone correct me please?
You can only call a class constructor once, you are first calling the no argument constructor when you created that horrible global variable. You then try to call the parameterized constructor in main().

I get that I am re declaring it. T he question really is how do I declare object driver that is an array and then populate it using a constructor? I do have to declare an array, don't I?
I do have to declare an array, don't I?

Possibly, but I'd use a std::vector instead, then push each "driver" into the vector.

T he question really is how do I declare object driver that is an array and then populate it using a constructor?

You can only call a class constructor once. Because you created the global variable calling the no argument constructor you get the default initialization. You can only initialize once, after that you're left with just a few options.

1. Create a setter function that assigns values to the various variables.
2. Copy another "driver" into your array.
3. Stop using the global variable and use the initialization list method of initializing the array.

Topic archived. No new replies allowed.