0

I am trying to define a 3-D array in my main-loop, then pass it to my class constructor so that it can be assigned to a new variable when my class constructor is called. I'm having trouble with how to do this, the error I'm receiving:

error: incompatible types in assignment of ‘float (*)[1][3]’ to ‘float [1][1][3]’ uav_wpts = waypoints;

And my code (I just tried to isolate the problem here):

"uav_gnc.cpp"

int main(int argc, char **argv)
{
    ros::init(argc, argv, "uav_gnc");
    std::vector<std::thread> uav_thread_vec; 

    // define 3-D array: rows are UAVs, columns are waypoints (each dimension 3)
    float waypoints[num_quads][wpt_num][3] = {
        {{-3.0, -2.0, 2.5}}
    }; 

    // Spawn quacopter objects in own threads, put threads in a vector
    for(int i=1; i<=(num_quads); i++){
        std::thread tn(test_call, i, waypoints);
        uav_thread_vec.push_back(std::move(tn));
    }

    // Cleanup
    for (std::thread & th : uav_thread_vec)
    {
        if(th.joinable()){
            th.join();
        }
    }

    return 0;
}

"My thread function"

void test_call(int id, float waypoints[][wpt_num][3]){
    // float uav_specific_waypoints = waypoints[(id-1)];
    std::string quad_id = std::to_string(id);
    std::string node_name = "uav"+quad_id;

    ROS_INFO_STREAM("Spawn thread: "+node_name);
    ros::NodeHandle nh(node_name);
    GNC uav(&nh, id, waypoints, wpt_num);

    run_quad_ops(&uav);
}

"My class: uav_gnc.h"

class GNC
{
private:
    int uav_num;
    int total_wpts;
    int curr_wpt = 0;
    float uav_wpts[num_quads][wpt_num][3];

public:
    quad_lab::cc_state cc_cmd;
    GNC(ros::NodeHandle *node_handle, int id, float waypoints[num_quads][wpt_num][3], int wpts);

//
// Member Functions
//

GNC::GNC(ros::NodeHandle *node_handle, int id, float waypoints[][wpt_num][3], int wpts):nh_(*node_handle)
{
    uav_wpts = waypoints;
    total_wpts = wpts;
    uav_num = id;
}

It would be fantastic if I knew how to take a specific row of my 3-D array and pass that by reference into my thread call and then into my class constructor and finally assign it to uav_wpts. Maybe I'm naive, or maybe I've spent too much time with Python. Can this be done?

Thanks for the help!

@François Andrieux, I also need to know how to pass this array by reference into my class constructor.

varlotbarnacle
  • 821
  • 1
  • 6
  • 7
  • C++ arrays don't let you copy them very easily. You need to do a per-element copy. You can just use [`std::array`](https://en.cppreference.com/w/cpp/container/array) instead which acts like any other value type. – François Andrieux Mar 13 '19 at 14:32
  • If you can I would suggest getting a matrix library or writing your own matrix class. Then you have a "first class" object that is easy to pass around, instead of raw arrays which have a lot of complications. – NathanOliver Mar 13 '19 at 14:32

0 Answers0