Error when initializing a struct with a brace-enclosed initializer list

15,403

Your class has a constructor, so it isn't an aggregate, meaning you cannot use aggregate initialization. You can add a constructor taking the right number and type of parameters:

struct CLICKABLE
{
  int x;
  int y;
  BITMAP* alt;
  BITMAP* bitmap;

  CLICKABLE(int x, int y, BITMAP* alt, BITMAP* bitmap) 
  : x(x), y(y), alt(alt), bitmap(bitmap) { ... }

  CLICKABLE() : x(), y(), alt(), bitmap() {}

};

Alternatively, you can remove the user declared constructors, and use aggregate initialization:

CLICKABLE a = {};         // all members are zero-initialized
CLICKABLE b = {1,2,0,0};
Share:
15,403

Related videos on Youtube

user2390934
Author by

user2390934

Updated on June 18, 2022

Comments

  • user2390934
    user2390934 about 2 years
    struct CLICKABLE
    {
        int x;
        int y;
        BITMAP* alt;
        BITMAP* bitmap;
    
        CLICKABLE()
        {
            alt=0;
        }
    };
    
    CLICKABLE input={1,2,0,0};
    

    This code gives me the following error:

    Could not convert from brace-enclosed initializer list

    Could someone explain me why the compiler is giving me this error, and how I can fix it? I'm still learning the language.

  • Hi-Angel
    Hi-Angel almost 9 years
    Actually the aggregate initialization could be used with user declared constructors too, like in this example with the presence of the first constructor.
  • juanchopanza
    juanchopanza almost 9 years
    @Hi-Angel Although the syntax is the same, it isn't "aggragate initialization" if the type isn't an aggregate. I think it is called something like "list initialization", which is a superset of aggregate initialization.

Related