user2390934
user2390934

Reputation: 249

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

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.

Upvotes: 10

Views: 17834

Answers (1)

juanchopanza
juanchopanza

Reputation: 227608

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};

Upvotes: 17

Related Questions