Storing data in Vector of Struct

Hello,

I am writing a program using ROS Workspace. There is a structure of data and which is converted to std::vector in somewhere of the codes. I am trying to store the data gained from one of the getters back to the vector.

When I compile the program, there is an error saying "no match for 'operator='" for the line of codes in getBaseRangeToBogie function.

I have pasted the concerning parts of my coding. Could you please tell me how can I solve it?

Thanks in advance.

1
2
3
4
5
6
7
8
9
#include <vector>

struct RangeVelocityStamped { 
  double range;
  double velocity;
  long timestamp;
};

std::vector<RangeVelocityStamped> rangeVelocityToBogiesFromBase(void);



1
2
3
4
5
6
7
8
9
class Base
{
public:
    Base();
    void getRangeBaseToBogie(const std::shared_ptr<Simulator> &sim, std::vector<RangeVelocityStamped> &vec, Info &info);

private:
    RangeVelocityStamped baseToBogie;
};



1
2
3
4
5
6
7
8
9
10
11
12
13
Base::Base()
{}

void Base::getRangeBaseToBogie(const std::shared_ptr<Simulator> &sim, std::vector<RangeVelocityStamped> &vec, Info &info)
{
    baseToBogie = sim->rangeVelocityToBogiesFromBase();
    vec.push_back(baseToBogie);
}

RangeVelocityStamped Base::getRangeBogieFromBase()
{
    return baseToBogie;
}
Last edited on
It's [code], not [c0de]. We're not been l33t here.

What does Simulator::rangeVelocityToBogiesFromBase() return?
It returns RangeVelocityStamped (the range in metres) which is
1
2
3
4
5
struct RangeVelocityStamped {
  double range; 
  double velocity; 
  long timestamp;
};
Well, then no idea. Can you post the entire error message from the compiler?
The error message is as follows:

1
2
3
/home/user/Desktop/pfms-2020a-TheingarAungThan/scratch/a3_skeleton/base.cpp:17: error: no match foroperator=’ (operand types are ‘RangeVelocityStamped’ and ‘std::vector<RangeVelocityStamped>’)
     baseToBogie = sim->rangeVelocityToBogiesFromBase();
                 ^
Last edited on
Oh, now I see it. Simulator::rangeVelocityToBogiesFromBase() return an std::vector<RangeVelocityStamped> not a RangeVelocityStamped.

This:
1
2
    baseToBogie = sim->rangeVelocityToBogiesFromBase();
    vec.push_back(baseToBogie);
doesn't make sense. rangeVelocityToBogiesFromBase() is returning multiple velocities and you're assuming that it returns a single velocity. You need to decide what to do with all of them. Do you want to append the velocities to vec? Which velocity should be assigned to baseToBogie?
Oh, I see now. Thank you for your explanation. So, I changed the codes as the following. Could you please check and let me know that it is correct or not?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <vector>

struct RangeVelocityStamped {
  double range; 
  double velocity; 
  long timestamp;
};

struct RangeVelocityStamped { 
  double range;
  double velocity;
  long timestamp;
};

std::vector<RangeVelocityStamped> rangeVelocityToBogiesFromBase(void);


1
2
3
4
5
6
7
8
9
class Base
{
public:
    Base();
    void getRangeBaseToBogie(const std::shared_ptr<Simulator> &sim, std::vector<std::vector<RangeVelocityStamped>> &vec, Info &info);

private:
    std::vector<RangeVelocityStamped> baseToBogie;
};


1
2
3
4
5
6
7
8
9
10
11
12
13
Base::Base()
{}

void Base::getRangeBaseToBogie(const std::shared_ptr<Simulator> &sim, std::vector<std::vector<RangeVelocityStamped>> &vec, Info &info)
{
    baseToBogie = sim->rangeVelocityToBogiesFromBase();
    vec.push_back(baseToBogie);
}

std::vector<RangeVelocityStamped> Base::getRangeBogieFromBase()
{
    return baseToBogie;
}
As far as I can tell, that will compile, but is that really what you want the code to do?
Topic archived. No new replies allowed.