mardi 23 août 2016

Why does `std::vector

This question already has an answer here:

This got me stumped. I have already (successfully) used std::vector multiple times, but now I am trying to create a vector that starts with several objects in it, so I use:

std::vector <int> v1(3);

This line is actually copied from the Microsoft documentation (http://ift.tt/2bfH1A7, in examples), but it does not even compile.

The error message I get is C2059 syntax error: 'constant'. If I hover the mouse over the 3 (which has the red wiggle), it says 'expected a type specifier' (???), and if I try to give it a type, just for chuckles, which makes no sense at all:

std::vector <int> v1(int);

it fails with C2334 unexpected token(s) preceding '{'; skipping apparent function body.

it seems to expect a function declaration - but I want a vector.

Here a complete piece:

#include <vector>

void main(void) {}

class T
{
  std::vector<int> v1(3);
};

Edit: I tried with other types as well, the problem is not only for int vectors. I assume I am missing something really silly.. but what?

Further Edit: This is not a duplicate of Why can in-class initializers only use = or {}?. I am not initializing the int vector with 3, I am trying to make a vector of 3 elements (that would be default initialized)

Aucun commentaire:

Enregistrer un commentaire