0

SOLVED! (See Edit)

I am trying to initialize an couple of arrays that are private members of a class. I am trying to use a public function to initialize these private arrays. My code looks like this:

void AP_PitchController::initGains(void){

_fvelArray[] = {20,     25,   30,    60,  90, 130, 160, 190, 220, 250, 280};
_kpgArray[]  = {6.0,   6.0,  8.0,   4.0, 3.0, 2.5, 2.5, 2.5, 2.5, 2.5, 2.5};
_kdgArray[]  = {2000, 2000, 1900,   300, 300, 200, 200, 200, 200, 200, 200};
_kigArray[]  = {0.1,   0.1,  0.2,  0.25, 0.3, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5};

}

These arrays are found in the header file AP_PitchController where they are declared private. When I try to compile the code, I get one of these messages for each initialization:

/../AP_PitchController.cpp:106: error: expected primary-expression before ']' token /../AP_PitchController.cpp:106: error: expected primary-expression before '{' token /../AP_PitchController.cpp:106: error: expected `;' before '{' token

And here are my private declarations:

Private:
uint8_t _fvelArray[];
float _kpgArray[];
float _kdgArray[];
float _kigArray[];

Does anyone know what I am doing wrong to initialize these arrays upon the call of initGains()?

EDIT:

I found the answer in one of the related questions.

All i need to do is provide an array size for the initialization:

static float _kpgArray[11];

And then initialize it outside of a function in the .cpp file:

uint8_t AP_PitchController::_fvelArray[11] = {20, 25, 30, 60, 90, 130, 160, 190, 220, 250, 280};

Thank you for your input!

3
  • 1
    private has nothing to do with these errors. Commented Feb 5, 2014 at 20:36
  • 1
    Have a look at the Related questions to the right. Commented Feb 5, 2014 at 20:37
  • Does this answer your question? How to initialise a member array of class in the constructor? Commented Dec 23, 2019 at 17:31

3 Answers 3

1

You can only use initialization syntax at declaration:

float _array[2] = {0.1f, 0.2f};

After it is declared you will have to initialize the members individually:

_array[0] = 0.1f;
_array[1] = 0.2f;

Or you could do it in a loop:

float temp[2] = {0.1f, 0.2f};
for( int i = 0; i < 2; ++i )
    _array[i] = temp[i];
Sign up to request clarification or add additional context in comments.

Comments

1

First, you cannot use the the initialization-list syntax that you're using since you've already declared your arrays (e.g. uint8_t _fvelArray = { ... }; would be valid when you first declare it under private: but _fvelArray = { ... }; is not valid in your initGains method). You must also declare the size of each array in your private declarations:

private:
    uint8_t _fvelArray[10]; // or whatever size you want

Once you've taken those steps, you can populate the arrays:

_fvelArray[0] = 20;
_fvelArray[1] = 25;
// ...

Is there a reason you don't initialize your arrays right away? Will the gain values change? Your method is called initGains after all. If not, use the initializer-list syntax at the point of declaration:

private:
    uint8_t _fvelArray[] = {20, 25, 30, 60, 90, 130, 160, 190, 220, 250, 280};

1 Comment

I didn't downvote, but your answer is not correct. these arrays are class variables and you can only use that syntax at declaration. In that function, said class variables are not being declared. Also, dropping [] wouldn't help.
0

You have several issues here:

  1. uint8_t _fvelArray[]; does not declare an array, but a pointer, the same as uint8_t *_fvelArray;. If you want to declare fixed-size array, you need to write uint8_t _fvelArray[11]; or in c++11 std::array<uint8_t, 11> _fvelArray;. For variable-length array you should use std::vector<uint8_t> _fvelArray;.
  2. {20, ...} expression is an initializer-list an cannot be used for array initialization outside of its definition. That means that you can write uint8_t _fvelArray_tmp[] = {20, ...}; and then copy it to your variable : memcpy (_fvelArray, _fvelArray_tmp, sizeof (_fvelArray_tmp)); but not to initialize some already existing variable. But if you use std::array or std::vector for _fvelArray type, you could simply write _fvelArray = {20, ...}; (but it only works for c++11).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.