2

Okay, I'm pretty new when it comes to C++ (having moved over from VB) and I can't seem to work out how to populate an array without doing:

array[0] = 1
array[1] = 2 

etc etc

So as could be expected, I have tried this:

float posVector[3]; //Declared outside of int main() as needs to be global
posVector = {0.0f,0.0f,0.0f}; //Inside int main()

Which is causing this error to be displayed:

extended initializer lists only available with -std=c++0x or -std=gnu++0x [enabled by default]

Nothing I have been able to locate online so far has been of any help. So any advice you can give me on fixing this will be appreciated!

2
  • What's the type of posVector? Commented Sep 29, 2015 at 20:54
  • God knows, it's whichever one is used in the online Zero Robotics Tournament IDE at zerorobotics.mit.edu Commented Sep 29, 2015 at 20:57

4 Answers 4

5

Once the array is declared it cannot be initialized using the initializer list. You can go with

float posVector[] = {0.0f,0.0f,0.0f};

or even better go with std::vector:

#include <vector>

std::vector<float> posVector = {0.0f,0.0f,0.0f};
Sign up to request clarification or add additional context in comments.

8 Comments

Right okay, so how do I make it globally available if it needs to be populated at run time? Can you please elaborate on std::vector?
The vector can be placed outside a function. What exactly are you looking for?
I'm trying to create an array which will be globally available and hold the current x,y,z values for the position of a sattelite
Try to avoid global variables. When a lot of functions are involved it's hard to remember which function should write or just read from/to the array. When threading comes into the game it becomes even more horrible. Consider to abstract and encapsulate your data. You use C++, so use what it offers you.
Okay... Sounds scary, but I'll do some research and give it a shot, I know scope should be limited as far as possible.
|
1

It can be done like this:

float f[] = {1.0, 2.0, 3.0, 4.0, 5.0};

Comments

0

Shortest way i think: float posVector[] { 0.0f,0.0f,0.0f };

Comments

0

As far as I understand, the question is not about INITIALIZING the array, but about assigning the array after it was already created. The answer is 'no way'. Arrays can not be assigned after they are initialized. However, arrays of fixed sizes some time might be more adequately presented as structs, and this is exactly the case! Coordinates are much better presented as structs!

Not commenting on the neccessity of global variable (most likely, not needed) here is the way to make it work:

struct Coordinate {
    float x;
    float y;
    float z;
};

Coordinate coord;

void foo() {
    coord = {1, 2, 3};
}

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.