0

I am trying to initialize an array of objects. This seems to fail in Keil, using the armcc compiler, using the --cpp11 flag.

The following is my class Wheel which would be used later in the Vehicle class.

class Wheel
{
public:
    Wheel(const uint8_t diameter);
private:
    uint8_t m_diameter;
};

The Vehicle class employs an array of Wheel, and these are initialized within the constructor. I would like to ideally initialize it like this:

class Vehicle
{
public:
    Vehicle();

private:
    Wheel wheels[3];
};

//in Wheel.cpp
// Constructor 
Vehicle::Vehicle()
    : wheels{Wheel(3),Wheel(4),Wheel(5)} 
{}

The other option would be to initialize everything within the constructor body, like this:

Vehicle::Vehicle(){
wheel[0] = Wheel(3);
wheel[1] = Wheel(4);
wheel[2] = Wheel(5);}

The compiler documentation mentions:

ARM Compiler supports initializer lists and uniform initialization, but the standard library does not provide an implementation of std::initializer_list. With a user-supplied implementation of std::initializer_list initializer lists and uniform initialization can be used.

How can I define an initializer list for my usecase?

mmcblk1
  • 158
  • 1
  • 3
  • 10
  • 1
    The second option is no viable, as `wheels` is first initialized with 3 "empty" wheels, and `Wheel` doesn't have default constructor. – Jarod42 Mar 02 '20 at 14:31
  • 1
    You could use `std::array wheels;` instead of `Wheel wheels[3];` – Thomas Sablik Mar 02 '20 at 14:31
  • @Jarod42 Yes, thats right. That's the reason I want to avoid it. – mmcblk1 Mar 02 '20 at 14:34
  • 1
    That said, OP, are you getting an actual error? `wheels{Wheel(3),Wheel(4),Wheel(5)}` should not be using a `std::initializer_list`. – NathanOliver Mar 02 '20 at 14:36
  • @ThomasSablik, unfortunately, the standard lib of armcc does not support `std::array`. – mmcblk1 Mar 02 '20 at 14:37
  • @NathanOliver, Keil doesn't throw an error during compilation. I see it compilers the file and then during linking, I see an error saying the object file for the corresponding .cpp file is missing. – mmcblk1 Mar 02 '20 at 14:40
  • @NathanOliver: When I run the build process with CMake, I see the following error: `fatal error U1077: 'C:\Keil_v5\ARM\ARMCC\bin\armcc.exe' : return code '0xc0000005'`, on compiling Class source file. – mmcblk1 Mar 02 '20 at 14:41

0 Answers0