-1

Trying to construct a class attribute - a vector of certain size

class cTest
{
    public:
    std::vector<double> myTable(1900);
};

main()
{
    cTest test;

    return 0;
}

compiler says:

./src/main.cpp:43:33: error: expected identifier before numeric constant
   43 |     std::vector<double> myTable(1900);
  |                                     ^~~~
ravenspoint
  • 19,093
  • 6
  • 57
  • 103

1 Answers1

0

This:

    std::vector<double> myTable(1900);

Looks like a member function declaration, and function declarations expect an argument list. 1900 does not satisfy that.

There are a couple of ways to solve this:

class cTest
{
    public:
    std::vector<double> myTable = std::vector<double>(1900);
};

If you want to make this a little less obnoxiously verbose:

class cTest
{
    using dbl_vec = std::vector<double>;

    public:
    dbl_vec myTable = dbl_vec(1900);
};

Or using the member initializer list in a constructor:

class cTest
{
    public:
    std::vector<double> myTable;

    cTest() : myTable(1900) { }
};
Chris
  • 26,361
  • 5
  • 21
  • 42