GeorgicaFaraFrica
GeorgicaFaraFrica

Reputation: 87

std::vector<bool> initialization returns `expected a type specifier`

I am trying to initialize a bool vector as a private parameter of a class and the only method that works is this:

class S
{
    std::vector<bool> _array{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
};

I have found online a nice way of initializing a vector and I have tried using it:

class S
{
    private:
      std::vector<bool> _array(24, false);
};

but it returns expected a type specifier for both 24 and false;

What do you think ?

Upvotes: 0

Views: 724

Answers (2)

Some programmer dude
Some programmer dude

Reputation: 409136

Inside classes or structures, definitions like

std::vector<bool> vect(24, false);

are considered function declarations.

Member inline initialization is only allowed to use uniform initialization with curly-braces {}, or "assignment" syntax as shown in the answer from Jarod42.

Upvotes: 1

Jarod42
Jarod42

Reputation: 217085

You might do (with parents)

std::vector<bool> _array = std::vector<bool>(24, false); // size is 24

With {}, you would use another constructor to take the whole list of item:

std::vector<bool> _array = std::vector<bool>{24, false}; // 24 for `true` but narrowing conversion

Syntax

std::vector<bool> _array (24, false); // Not allowed for member initialization

but you could use it in constructor

struct S
{
    std::vector<bool> _array;
    S() : _array(24, false) {}
    // ...
};

Upvotes: 2

Related Questions