How to access member of vector of struct?

May 31, 2020 at 5:20pm
I have some struct like the followings:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
struct Pose{
    double x;
    double y;
    double orientation;
}

struct Aircraft{
    Pose pose;
    double velocity;
}

struct Container{
    std::vector<Aircraft> a;
    std::mutex access;
}


Then I create a class based on Container as the following:

1
2
3
4
5
6
class Flight:public Container
{
    public:
    Flight();
    void getData();
}


And in the definition of the method above, I write the following codes.

1
2
3
4
5
6
7
Flight::Flight()
{}

void Flight::getData()
{
    a.pose = [something];
}


But, I am getting an error at "a.pose" and saying that pose is not a member. So, how can I access that "pose"?
May 31, 2020 at 5:33pm
You can't just access a.pose.
You have to access a[some index].pose.

Which requires that your a vector is filled with at least one Aircraft.

If you want a to be a single Aircraft, then don't make it a vector.
Last edited on May 31, 2020 at 5:38pm
May 31, 2020 at 5:40pm
Is there any other method that I can solve the error without changing that Aircraft vector?
Because that vector is the skeleton code and I am not supposed to make changes to it.
Jun 1, 2020 at 3:41am
same point as asked http://www.cplusplus.com/forum/general/267347/

class, struct is just template type, static on compile time so you must conceive such and solve it in a way of run time / dynamically, as it's vector or all dynamic containers.
initialize it by use of its constructor
Jun 1, 2020 at 1:06pm
same point as

No.


@theingar:
Flight is a container that has aircrafts.

The getData() could add aircraft(s) to flight or update aircraft(s) that the flight already has.
You have to know what you want it to do.

Ignoring the classes, you have a vector:
1
2
3
4
5
6
7
8
9
10
11
std::vector<Aircraft> a;
Aircraft b;

a.push_back( b ); // add an aircraft to a

// modify every aircraft in a
for ( auto& c : a ) {
  c = b;
  // or
  c.velocity = 3.14;
}
Jun 3, 2020 at 4:09pm
Suppose there are 3 Aircraft in the a vector. You say you want to change a.pose. Which aircraft's pose member do you want to change?

I hope this will help you see that what you're trying to do doesn't make sense, at least not with the data that you've presented.

If this is an assignment, then please post the full assignment.
Topic archived. No new replies allowed.