Friday, July 13, 2018

c++ - How to pass a 3-D array into an class constructor

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 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.

No comments:

Post a Comment

plot explanation - Why did Peaches&#39; mom hang on the tree? - Movies &amp; TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...