如何将3-D数组传递给类构造函数

时间:2019-03-13 14:25:50

标签: c++ arrays class object

我试图在主循环中定义一个3-D数组,然后将其传递给我的类构造函数,以便在调用我的类构造函数时可以将其分配给新变量。我在执行此操作时遇到了麻烦,收到的错误是:

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

还有我的代码(我只是想在这里隔离问题):

“ 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;
}

“我的线程功能”

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);
}

“我的课程: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;
}

如果我知道如何获取3-D数组的特定行并将其通过引用传递到我的线程调用中,然后传递到我的类构造函数中,最后将其分配给uav_wpts,那将是很棒的。也许我很天真,或者我花了太多时间在Python上。能做到吗?

感谢您的帮助!

@FrançoisAndrieux,我还需要知道如何通过引用将该数组传递给我的类构造函数。

0 个答案:

没有答案