为什么我的2D数组不被接受作为有效参数?

时间:2017-03-31 05:51:49

标签: c++ arrays

我一直试图解决由于我的2D阵列导致的未定义的参考错误数小时,但我不知道我缺少什么。我有一个2D数组。

views

JointedLeg.h:

#include <iostream>
#include "ServoJoint.h"
#include "JointedLeg.h"
#include "TwoJointLeg.h"
#include "ThreeJointLeg.h"

int main() {

    //2D array containing the 7 calibrated positions for each of the 10 servo motors
    size_t servoCalibrations[10][7]={{143, 190, 245, 305, 365, 410, 467},
                                     {143, 205, 260, 320, 377, 423, 475},
                                     {115, 168, 230, 280, 340, 392, 440},
                                     {130, 182, 235, 290, 340, 397, 455},
                                     {135, 190, 235, 290, 345, 400, 455},
                                     {140, 190, 250, 305, 355, 415, 465},
                                     {130, 185, 240, 300, 355, 415, 465},
                                     {140, 200, 245, 300, 355, 410, 460},
                                     {140, 200, 245, 300, 355, 410, 460},
                                     {140, 200, 245, 300, 355, 410, 460}};

    double walkCycle[4][2]= {{45, 45},
                            {60, 150},
                            {60, 135},
                            {30, 60}};

    //Array of pointers to 10 size_t arrays of size 7
    //This is because C++ doesn't allow accessing an entire row/column of a 2D array
    //or I don't know how to
    size_t * calibrationArrays[10];
    for(size_t i=0; i<10; i++){
        size_t * tmp= new size_t[7];
        for(size_t j=0; j<7; j++){
            tmp[j]= servoCalibrations[i][j];
        }
        calibrationArrays[i]= tmp;
    }

    //Array of pointers to 10 ServoJoint objects
    ServoJoint * servoJoints[10];

    //Dynamically allocate 10 ServoJoint objects
    for(size_t i=0; i<10; i++){
        servoJoints[i]= new ServoJoint(i, 7, calibrationArrays[i]);
    }

    for(size_t i=0; i<10; i++){
        std::cout << "Servo motor " << i+1 << " has the following PWM values: ";
        for(size_t j=0; j<7; j++){
            std::cout << servoJoints[i]->getPWM(-90+(double)(j*30)) << " ";
        }
        std::cout<< std::endl;
    };

    //Array of pointers to 2 ThreeJointLeg objects
    ThreeJointLeg * threeJointLegs[2];

    //Dynamically allocate 2 ThreeJointLeg objects
    for(size_t i=0; i<6; i+=3){
        threeJointLegs[i]= new ThreeJointLeg(servoJoints[i], servoJoints[i+1], servoJoints[i+2], 1-(i%2)*2, walkCycle);
    }

    //Array of pointers to 2 TwoJointLeg objects
    TwoJointLeg * twoJointLegs[2];

    //Dynamically allocate 2 TwoJointLeg objects
    for(size_t i=6; i<10; i+=2){
        twoJointLegs[i]= new TwoJointLeg(servoJoints[i],servoJoints[i+1], 1, walkCycle);
    }

    return 0;
}

JointedLeg.cpp:

#ifndef ROBOTLEG_JointedLeg_H
#define ROBOTLEG_JointedLeg_H

#include "ServoJoint.h"

class JointedLeg{

private:
//Pointers to the 2 ServoJoint objects for the upper and lower joint
ServoJoint * upperJoint;
ServoJoint * lowerJoint;

int isRight;

//2D array, where each of the four rows represents each phase in the walk cycle and each column of each row
//represents the upper and lower joint angles respectively

//walkCycle[0][]= home
//walkCycle[1][]= step forward
//walkCycle[2][]= step down
//walkCycle[3][]= step back
double walkCycle[4][2];

public:
JointedLeg(ServoJoint * joint1, ServoJoint * joint2, int side, double givenWalk[][2]);

TwoJointLeg.h:

#include "JointedLeg.h"

JointedLeg::JointedLeg(ServoJoint * joint1, ServoJoint * joint2, int side, double givenWalk[][2]):upperJoint(joint1), lowerJoint(joint2), isRight(side){
    for(size_t i=0; i<4; i++){
        for(size_t j=0; j<2; j++){
            walkCycle[i][j]= givenWalk[i][j];
        }
    }
}

TwoJointLeg.cpp:

#ifndef QUADRUPEDROBOT_TWOJOINTLEG_H
#define QUADRUPEDROBOT_TWOJOINTLEG_H

#include "JointedLeg.h"

class TwoJointLeg : public JointedLeg{

public:
    TwoJointLeg(ServoJoint * joint1, ServoJoint * joint2, int side, double givenWalk[][2]);

};

#endif //QUADRUPEDROBOT_TWOJOINTLEG_H

我想将此作为参数传递给以下构造函数:

#include "TwoJointLeg.h"

TwoJointLeg::TwoJointLeg(ServoJoint * joint1, ServoJoint * joint2, int side, double givenWalk[][2])
        :JointedLeg(joint1, joint2, side, givenWalk){}

我在Passing a 2D array to a C++ function上读到,当我声明一个带有2个括号的2D数组,即[] []时,我可以在函数头中执行相同操作并简单地传递数组变量名,这是我所相信的我做完了。

然而,我的编译器一直告诉我,我正在传递一个&#34; double(*)[2]。&#34;我知道2D数组在技术上是指向子数组的指针,并且第一个数组可以衰减为指针,但是当我在函数调用和定义中使用相同的语法时,为什么参数类型不匹配?

我已经在函数标题中尝试了这两个:

JointedLeg(ServoJoint * joint1, ServoJoint * joint2, int side, double givenWalk[][2]);

2 个答案:

答案 0 :(得分:0)

请参阅以下最小示例,其中显示了它的工作原理:

void JointedLeg(double givenWalk[][2]) { }

int main(int argc, char* argv[]) {

    double walkCycle[4][2]= {{45, 45},
        {60, 150},
        {60, 135},
        {30, 60}};

    JointedLeg(walkCycle);

    return 0;
}

答案 1 :(得分:0)

查看您在循环中使用的数组索引:

//Array of pointers to 2 ThreeJointLeg objects
ThreeJointLeg * threeJointLegs[2];

//Dynamically allocate 2 ThreeJointLeg objects
for(size_t i=0; i<6; i+=3){
    threeJointLegs[i/3]= new ThreeJointLeg(servoJoints[i], servoJoints[i+1], servoJoints[i+2], 1-(i%2)*2, walkCycle);
}

//Array of pointers to 2 TwoJointLeg objects
TwoJointLeg * twoJointLegs[2];

//Dynamically allocate 2 TwoJointLeg objects
for(size_t i=6; i<10; i+=2){
    twoJointLegs[(i-6)/2]= new TwoJointLeg(servoJoints[i],servoJoints[i+1], 1, walkCycle);
}