2

I want to initialize a std::vector to have a specific length. in the .hpp file I use

#include <vector>

class foo
{
foo(){};
~foo(){};
std::vector<double> pressure (4,0); //vector 4 elements = 0

void readPressure()
{
    pressure.at(0) = 1;
    pressure.at(1) = 2;
    pressure.at(2) = 3;
    pressure.at(3) = 4;
}
...
};

But I get the error:

error: expected identifier before numeric constant
std::vector<double> pressure(4,0);
                             ^
error: expected ',' or '...' before numeric constant

I read that this might be due to not using C++11 but I specify in my makefile

set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
set(CMAKE_CXX_FLAGS_RELEASE "-O3")

I am compiling on a raspberry 4, g++ version is 8.3

***edited the .hpp file

FR_MPI
  • 111
  • 1
  • 9
  • 1
    why are you putting this in a .hpp file ? this should go within some .cpp file. also try following declaration: auto pressure = std::vector(4, 0); to avoid most vexing parsing. – StPiere Oct 06 '20 at 08:43
  • Try running cmake --build build in verbose mode to be absolutely sure about your flags (`VERBOSE=1 cmake --build build`), because this codes compiles on me [reproductible](https://godbolt.org/z/P3PYY1). Also this is not an initializer list this puts 4 zeros to the vector , not the values 4 and then zero – Spyros Mourelatos Oct 06 '20 at 08:43
  • 3
    I suspect that it's actually a member of a class and you assumed that this detail was irrelevant. Please read about the [mcve]. – molbdnilo Oct 06 '20 at 08:48
  • 3
    What you posted [compiles](https://ideone.com/77oEFJ). Thus it is either not the problem, or what you posted isn't what you're actually doing. Posting a proper [mcve] that actually *reproduces* the problem *for us* using *only* what you posted is critical if you want us to help solve it. Unrelated, *"in the .hpp file I use..."* global decls don't belong in header files, at least not like that. So this strongly supports @molbdnilo crystal ball that this is buried in some class/template declaration that you completely neglected to mention.. – WhozCraig Oct 06 '20 at 08:49
  • yes this is inside a class. I didn't realize that was important. as I mentioned it does not compile for me and I have no idea why and not enough knowledge about c++ to pinpoint the error more or narrow it down, that's why I posted it here – FR_MPI Oct 06 '20 at 08:58
  • 2
    I retracted my comment, becuase your code seemed to work. Now that we can see the real code I'd like to add it again, because the problem is indeed [Most vexing parsing](https://stackoverflow.com/questions/14077608/what-is-the-purpose-of-the-most-vexing-parse). I'm pretty sure there is duplicate for this, but TL;DR; you need to initialize it in the constructor, the best would be to do it in the member initializer list . – Lukas-T Oct 06 '20 at 08:59
  • `std::vector pressure(4, 0)` -> `std::vector pressure { 0, 0, 0, 0 }` – Jabberwocky Oct 06 '20 at 09:09
  • Your CMake snippet doesn't show if you before or after declaring the target. You should follow the advice and make sure that the correct compiler flags are used. – fdk1342 Oct 06 '20 at 13:06

1 Answers1

3

Default member initializer (since C++11) only supports brace and equal-sign initializer. E.g.

class foo
{
    std::vector<double> pressure = std::vector<double>(4,0); //vector 4 elements = 0
    ...
};

BTW: We can't use braced-initializer as std::vector<double> pressure{4,0};, because it would initialize the vector containing 2 elements with value 4 and 0, which is not what you want.

songyuanyao
  • 169,198
  • 16
  • 310
  • 405