28

I have a line of code inside a class's private member variables:

vector<double> dQdt(3)

When compiling in xcode, this gives an error "expected parameter declarator." I think I provided sufficient info. I don't see anything wrong with this declaration.

roulette01
  • 1,984
  • 2
  • 13
  • 26

6 Answers6

29

You have to initialize the variable in the constructor's initializer list:

class X 
{
    private:
     vector<double> dQdt;
    public:
     X() : dQdt(3) {}
};
Raman
  • 2,735
  • 1
  • 26
  • 46
26

If you read e.g. this member initialization reference you will learn that default member initialization have to be a brace or equals initializer. I.e. you need to either use curly-braces:

std::vector<double> dQdt{ 0.0, 0.0, 0.0 };

or using the equals character:

std::vector<double> dQdt = std::vector<double>(3);

Since this was introduced with the C++11 standard, you need to enable that in your environment.

tambre
  • 4,625
  • 4
  • 42
  • 55
Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • 1
    But those aren't the same thing. The first creates a single element with value 3. The second creates 3 elements with default initialization (0.0 in this case). – ooga Feb 04 '20 at 22:09
  • 1
    @Someprogrammerdude, if you take [@Raman's anwser](https://stackoverflow.com/a/39560305/4561887) and combine it with your answer, you get the best answer! Then, it would clearly cover all 3 valid techniques: 1) use an initializer list in the class or struct constructor, as @Raman shows, OR 2) use the brace initializer `type var{some_default_val};` at the site of the variable declaration (like you show), OR 3) use the equals character `type var = some_default-val;` (like you also show). – Gabriel Staples May 15 '20 at 08:13
12

I got this error while trying to compile my C++ code having an initialized vector. Change the () to {} worked for me in the initialization part; Earlier my code looked like this:

vector<vector<int>> minA(11, vector<int>(11, INT_MAX));

I changed my code to replace circular brackets with curly braces and the error disappered.

vector<vector<int>> minA{11, vector<int>(11, INT_MAX)};
Yun
  • 3,056
  • 6
  • 9
  • 28
abhinav1602
  • 1,190
  • 17
  • 18
8

The parameter for constructors of data members should be written in the initializer list of your class' constructor. That is, instead of

class Foo {
    vector<double> dQdt(3);
};

You should write

class Foo {
public:
    Foo() : dQdt(3) {}
private:
    vector<double> dQdt;
};
timrau
  • 22,578
  • 4
  • 51
  • 64
5

As well as initializing in the initializer list of the constructor, you can initialize with a brace initializer list:

class Foo {
    vector<double> dQdt{3};
};

The actual text of the error is because the compiler was expecting you to declare a function, taking an argument of some type, and return the vector<double>. 3 is not a valid declaration of a parameter to a function.

-1

The question is already answered the following however works as well.
( Which might be more useful to assign initial values. For example 24 times the 42. )

const int default_value = 42;

struct foo
{
    vector<double> hour{vector<double>(24,default_value)};
};
NewNewton
  • 1,015
  • 1
  • 10
  • 22